R:如何将选择表达式列表(在本例中为字符串)传递给子集函数?

发布于 2024-08-27 23:13:14 字数 742 浏览 5 评论 0原文

这是一些示例数据:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-09-03 23:13:14

您可能需要 %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) 

You probably want the %in% operator

> 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) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文