dplyr用过滤器选择观察结果

时间:2015-11-10 08:46:57

标签: r dplyr

如何使用dplyr从下面的数据集中选择Giovanni Bianchi和Luca Rossi?

NAME          SURNAME   COLOR   ...
Giovanni      Rossi     Red 
Giovanni      Bianchi   Red 
Giovanni      Bianchi   Blue 
Luca          Rossi     Blue
Luca          Rossi     Red
Giovanni      Rossi     Red 

1 个答案:

答案 0 :(得分:2)

您可以使用paste创建全名,filter为新变量的子集

library(dplyr)

filter(d,paste(NAME,SURNAME) %in% c("Giovanni Bianchi","Luca Rossi"))

      NAME SURNAME COLOR
1 Giovanni Bianchi   Red
2 Giovanni Bianchi  Blue
3     Luca   Rossi  Blue
4     Luca   Rossi   Red

数据

d <- read.table(text="
NAME          SURNAME   COLOR
Giovanni      Rossi     Red 
Giovanni      Bianchi   Red 
Giovanni      Bianchi   Blue 
Luca          Rossi     Blue
Luca          Rossi     Red
Giovanni      Rossi     Red ",head=TRUE,stringsAsFactors=FALSE)