我现在重写我的问题以使其更清楚
我想替换这样的条件,其中var是dataframe(dataframe $ var)中的变量,带有粘贴或其他解决方案,因为我有很多条件值(?)(在我的示例中为a,b和c).
subdataframe<-dataframe[var=="a"|var=="b"|var=="c",]
Run Code Online (Sandbox Code Playgroud)
我试图制作condtion值的列表(?).
sample<-c("a","b","c")
Run Code Online (Sandbox Code Playgroud)
然后使用粘贴来制作逻辑条件
subdataframe<-dataframe[paste("var",sample,sep="==",collapse="|"),]
Run Code Online (Sandbox Code Playgroud)
但这不起作用
请帮助=)
马库斯
注意运气(106):
> fortune(106)
If the answer is parse() you should usually rethink the question.
-- Thomas Lumley
R-help (February 2005)
Run Code Online (Sandbox Code Playgroud)
所以我鼓励你重新思考你想要做的事情......
我猜你可以使用match或%in%达到你想要的结果,但你没有告诉我们你想要做什么.
> sample <- c("a","b","c")
> var <- c("a","d","c")
> eval(parse(text=paste("var==",sample,"",sep="'",collapse="|")))
[1] TRUE FALSE TRUE
> var %in% sample
[1] TRUE FALSE TRUE
Run Code Online (Sandbox Code Playgroud)