R:如何将选择表达式列表(在本例中为字符串)传递给子集函数?
这是一些示例数据:
data = data.frame(series = c("1a", "1b", "1e"), reading = c(0.1, 0.4, 0.6))
> data
series reading
1 1a 0.1
2 1b 0.4
3 1e 0.6
我可以使用子集提取选择性单行:
> subset (data, series == "1a")
series reading
1 1a 0.1
并使用逻辑 OR 提取多行
> subset (data, series == "1a" | series == "1e")
series reading
1 1a 0.1
3 1e 0.6
但是如果我有一长串系列表达式,这对输入来说真的很烦人,所以我更愿意以更好的方式定义它们,像这样:
series_you_want = c("1a", "1e") (although even this sucks a little)
并且能够做这样的事情,
subset (data, series == series_you_want)
上面的方法显然失败了,我只是不确定最好的方法是什么?
Here is some example data:
data = data.frame(series = c("1a", "1b", "1e"), reading = c(0.1, 0.4, 0.6))
> data
series reading
1 1a 0.1
2 1b 0.4
3 1e 0.6
Which I can pull out selective single rows using subset:
> subset (data, series == "1a")
series reading
1 1a 0.1
And pull out multiple rows using a logical OR
> subset (data, series == "1a" | series == "1e")
series reading
1 1a 0.1
3 1e 0.6
But if I have a long list of series expressions, this gets really annoying to input, so I'd prefer to define them in a better way, something like this:
series_you_want = c("1a", "1e") (although even this sucks a little)
and be able to do something like this,
subset (data, series == series_you_want)
The above obviously fails, I'm just not sure what the best way to do this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要
%in%
运算符You probably want the
%in%
operator