我有一个矢量转换术语,我想用它作为参数来对数据帧进行子集化。这是一个简短的例子:
#this is my data frame
dat <- data.frame(x=c(1:5), y=c("a","b","c","d","e"), z=c(7,13,20,27,33))
#this is the vector I want to use as subsetting parameter:
w=c("c","d","e")
#and I would like to get in return all the associated columns, like this:
dat_subset <- data.frame(x=c(3:5), y=c("c","d","e"), z=c(20,27,33))
我有一个非常大的术语列表,因此无法输入所有术语以供选择。
谢谢!
答案 0 :(得分:1)
也许您只是在寻找%in%
。我假设“w”中的值应该来自你的“y”列。
dat[dat$y %in% w, ]
# x y z
# 3 3 c 20
# 4 4 d 27
# 5 5 e 33