简体   繁体   English

通过列上的列表子集 dataframe

[英]subset a dataframe by a list on column

I have a similar dataframe, lets say df我有一个类似的 dataframe,让我们说df

x1    x2    x3    
Tom   Grad  35K  
Ada   Secon 21K
John  Grad  47K
Vic   PhD   52K

That I need to subset for a list of terms, as for example: listOfTerms <- c("John", "Nash", "Vic", Mary") , which should be checked against df$x1 , resulting in an output which shows me the relevant rows. I am having hard time to figure out how can it be done, could you please help me?我需要对术语列表进行子集化,例如: listOfTerms <- c("John", "Nash", "Vic", Mary") ,应该对照df$x1检查,从而产生 output向我展示了相关行。我很难弄清楚该怎么做,你能帮我吗?

Here's an option.这是一个选项。

library(dplyr)

mydata <- data.frame(x1 = c("Tom", "Ada", "John", "Vic"), 
                     x2 = c("Grad", "Secon", "Grad", "PhD"),
                     x3 = c("35k", "21k", "47k", "52k"))

listOfTerms <- c("John", "Nash", "Vic")

subset <- mydata %>%
  filter(x1 %in% listOfTerms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM