R:如何将选择表达式列表(在这种情况下为字符串)传递给子集函数? [英] R: How to pass a list of selection expressions (strings in this case) to the subset function?

查看:63
本文介绍了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

我可以使用子集提取选择性的单行:

Which I can pull out selective single rows using subset:

> subset (data, series == "1a")
  series reading
1     1a     0.1

并使用逻辑或拉出多行

> 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)

并能够执行类似的操作

subset (data, series == series_you_want)

以上内容显然失败了,我不确定执行此操作的最佳方法是什么?

The above obviously fails, I'm just not sure what the best way to do this is?

推荐答案

您可能需要%in%运算符

> dat <- data.frame(series = c("1a", "1b", "1e"), reading = c(0.1, 0.4, 0.6))
> series_you_want <- c("1a", "1e")
> subset(dat, series %in% series_you_want) 

这篇关于R:如何将选择表达式列表(在这种情况下为字符串)传递给子集函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆