当R中某些列中的值匹配时过滤行

时间:2019-07-12 19:54:27

标签: r

我想过滤数据框以仅包含在某些列中具有匹配值的行。

我的数据:

df <- data.frame("Date" = ymd(c("2005-01-01", "2005-01-02", "2005-01-02", "2005-01-01", "2005-01-01")),
                 "Person" = c("John", "John", "John", "Maria", "Maria"),
                 "Job" = c("OR", "ER", "Heart", "Liver", "CV"),
                 "Type" = c("Day", "Night", "Night", "Day", "Night"))

我想创建一个较小的数据框,其中包含与日期,人员和类型匹配的行。

我要看的数据框是这样的:

df1 <- data.frame("Date" = ymd(c("2005-01-02", "2005-01-02")),
                  "Person" = c("John", "John"),
                  "Job" = c("ER", "Heart"),
                  "Type" = c("Night", "Night"))

1 个答案:

答案 0 :(得分:1)

我们可以使用group_by中的filterdplyr

library(dplyr)

df %>%
  group_by(Date, Person, Type) %>%
  filter(n() > 1)

输出:

# A tibble: 2 x 4
# Groups:   Date, Person, Type [1]
  Date       Person Job   Type 
  <date>     <fct>  <fct> <fct>
1 2005-01-02 John   ER    Night
2 2005-01-02 John   Heart Night