Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当运算符作为字符串时R中的Vlookup_R - Fatal编程技术网

当运算符作为字符串时R中的Vlookup

当运算符作为字符串时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

我有一个看起来像这样的数据框,它有一个带有运算符和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     <= 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])