R 子集具有多个键的数据帧 [英] R subset a data frame with multiple keys
本文介绍了R 子集具有多个键的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框
id val
a 1
a 2
a 3
b 4
b 5
c 6
我想使用 id 的子集来查找此数据框的子集.我知道如果子集标准仅为 1 个值,例如
I would like to find a subset of this data frame using a subset of the id's. I know I can do the following if the subset criteria is just 1 value for e.g.
y = subset(x,id=='a')
但是,如果我有一组多个 id,我如何获得一个子集.例如 c('a','b').做
However how do I get a subset if I have a set of several ids. For example c('a','b'). Doing
y = subset(x,id==c('a','b'))
没有给我想要的.
推荐答案
您可以使用逻辑运算符进行子集化,例如
You can subset with logical operators, e.g.
y=subset(x,id=='a' | id=='b')
或者您可以使用 %in%
运算符:
or you can use the %in%
operator:
y=subset(x,id %in% c('a','b'))
这篇关于R 子集具有多个键的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文