我想从参数化的数据框中提取信息。 那就是:
A <- c(3, 10, 20, 30, 40)
B <- c(30, 100, 200, 300, 400)
DF <- data.frame(A, B)
DF[A%in%c(1, 2, 3, 4, 5), ] # it works
# But what if this is the case,
# which comes for example out of a user-menu selection:
m <- "A%in%"
k <- c(1, 2, 3, 4, 5)
# How can we make something like that work:
DF[eval(parse(text=c(m, k))), ]
答案 0 :(得分:2)
这有效:
DF[eval(parse(text = paste0(m, deparse(k)))), ]
# A B
#1 3 30
但是,应该避免使用eval(parse())
。也许这会是你的替代选择?
x <- "A"
fun <- "%in%"
k <- c(1, 2, 3, 4, 5)
DF[getFunction(fun)(get(x), k), ]
# A B
#1 3 30
答案 1 :(得分:2)
此外,
DF[eval(parse(text=paste(m, substitute(k)))),]
或
DF[eval(parse(text=paste(m, quote(k)))),]
或
DF[eval(parse(text=paste(m, "k"))),]