当运算符作为字符串时R中的Vlookup
我有一个看起来像这样的数据框,它有一个带有运算符和WoE值的切点值:当运算符作为字符串时R中的Vlookup,r,R,我有一个看起来像这样的数据框,它有一个带有运算符和WoE值的切点值: Cutpoint <- c("<= 0","<= 2","<= 4.5","<= 8","> 8","Missing") WoE <- c("0.12","0.24","0.45","0.55","0.92","0.99") dictionary <- data.frame(Cutpoint,WoE) Cutpoint WoE 1 <= 0 0.12 2
Cutpoint <- c("<= 0","<= 2","<= 4.5","<= 8","> 8","Missing")
WoE <- c("0.12","0.24","0.45","0.55","0.92","0.99")
dictionary <- data.frame(Cutpoint,WoE)
Cutpoint WoE
1 <= 0 0.12
2 <= 2 0.24
3 <= 4.5 0.45
4 <= 8 0.55
5 > 8 0.92
6 Missing 0.99
非常感谢您的提示。这可以通过去除赋值运算符并使用除最后2个切点之外的所有切点都是“R不是MS Excel”的信息来实现。vlookup不是一件事。您希望使用嵌套的ifelse
语句或开关
。
val_A <- c("a","a","b","b","c","c","c","d")
val_B <- c("6","-1","3",NA,"7","8",NA,"9")
table <- data.frame(val_A,val_B)
val_A val_B
1 a 6
2 a -1
3 b 3
4 b <NA>
5 c 7
6 c 8
7 c <NA>
8 d 9
val_A table_B
1 a 0.55
2 a 0.12
3 b 0.45
4 b 0.99
5 c 0.55
6 c 0.55
7 c 0.99
8 d 0.92
Cutpoint <- c("<= 0","<= 2","<= 4.5","<= 8","> 8","Missing")
WoE <- c("0.12","0.24","0.45","0.55","0.92","0.99")
## stringsAsFactors=FALSE is *essential* here -- or
## use options(stringsAsFactors=FALSE) to set globally
dictionary <- data.frame(Cutpoint,WoE,stringsAsFactors=FALSE)
val_A <- c("a","a","b","b","c","c","c","d")
val_B <- c("6","-1","3",NA,"7","8",NA,"9")
table <- data.frame(val_A,val_B,stringsAsFactors=FALSE)
cuts <- as.numeric(gsub("(<=|>)","",dictionary$Cutpoint))
cuts2 <- c(-Inf,head(cuts,-2),Inf) ## all but last 2 vals of 'cuts', + Inf
cc <- cut(as.numeric(table$val_B),breaks=cuts2)
cc2 <- replace(as.numeric(cc),is.na(cc),nrow(dictionary))
data.frame(val_A,table_B=as.numeric(WoE)[cc2])