dplyr:仅在满足不同列中的多个条件时进行过滤

时间:2017-09-11 16:21:00

标签: r dplyr filtering

这是我的df = myproject

myproject <- data.frame(
  Participant                = 1:5,
  `futuremw:1`               = c(1L, 2L, 1L, 1L, NA),
  `pastmw:1`                 = c(1L, 1L, 2L, 1L, NA),
  `proportionfuturepast:1`   = c(4L, 7L, 1L, 2L, NA),
  my_video_item_duration_min = c(5, 1, 7.02, 6, 6),
  check.names = FALSE
)

我想排除参与者的价值&#34; my_video_item_duration_min&#34;小于5且大于7.为此,我应用此dplyr代码:

myproject_filtered = myproject %>%
filter(my_video_item_duration_min > 5) %>% 
filter(my_video_item_duration_min < 7)

现在我希望每次futuremw排除一个参与者:1与2不同而pastmw:1等于1且proportionfuturepast与3不同,因此我希望排除参与者4,因为所有三个排除标准都在同时满足。如果仅满足1或2个排除标准但不满足另一个排除标准,则不排除参与者。 此外,我想继续包括参与者n。 5,即使它呈现NA值

我试过这个

myproject_filtered = myproject %>%
filter(my_video_item_duration_min > 5) %>%
filter(my_video_item_duration_min < 7) %>%
filter(futuremw_1 != 2 | pastmw_1 == 1 | proportionfuturepast_1 != 3) 

我使用了答案中提出的代码并且它有效。但是,我现在想要结合不同的排除标准。以下代码不起作用:

    myproject_excluding_participants = myproject %>%
  filter (
    my_video_item_duration_min >= 5,
    my_video_item_duration_min <= 7,
    ! complete.cases(.) | mind_wandering_1 != 1 | proportionMW_1 != 11,
    ! complete.cases(.) | mind_wandering_1 != 2 | proportionMW_1 == 11,
    ! complete.cases(.) | futuremw_1 != 2 | pastmw_1 != 2 | proportionfuturepast_1 == 4,
    ! complete.cases(.) | futuremw_1 != 1 | pastmw_1 != 2 | proportionfuturepast_1 == 1,
    ! complete.cases(.) | futuremw_1 != 2 | pastmw_1 != 1 | proportionfuturepast_1 == 7,
    ! complete.cases(.) | futuremw_1 != 1 | pastmw_1 != 1 | proportionfuturepast_1 != 1,
    ! complete.cases(.) | futuremw_1 != 1 | pastmw_1 != 1 | proportionfuturepast_1 != 7,
    ! complete.cases(.) | ED_1 != 1 | proportionED_1 != 11
    ! complete.cases(.) | ED_1 != 2 | proportionED_1 != 11,
    ! complete.cases(.) | proportionfuturepast_dailylife_1 != 1 | futureMW_dailylife_1 != 5,
    ! complete.cases(.) | proportionfuturepast_dailylife_1 != 1 | pastMW_dailylife_1 == 5,
    ! complete.cases(.) | proportionfuturepast_dailylife_1 != 7 | pastMW_dailylife_1 != 5,
    ! complete.cases(.) | proportionfuturepast_dailylife_1 != 7 | futureMW_dailylife_1 == 5,
    ! complete.cases(.) | futureMW_dailylife_1 != 5 | pastMW_dailylife_1 != 5 | proportionfuturepast_dailylife_1 == 4,
    ! complete.cases(.) | CurrentConcernsAreas_14 != 1 | SumCurrentConcernsAreas1to13 < 0
    )

1 个答案:

答案 0 :(得分:1)

如何反转你的逻辑来定义保持什么排除

library(dplyr)
myproject %>%
  filter(
    my_video_item_duration_min >= 5,
    my_video_item_duration_min < 7,
    ! complete.cases(.) | `futuremw:1` == 2 | `pastmw:1` != 1 | `proportionfuturepast:1` == 3
  )

您的数据:

myproject <- data.frame(
  Participant                = 1:5,
  `futuremw:1`               = c(1L, 2L, 1L, 1L, NA),
  `pastmw:1`                 = c(1L, 1L, 2L, 1L, NA),
  `proportionfuturepast:1`   = c(4L, 7L, 1L, 2L, NA),
  my_video_item_duration_min = c(5, 1, 7.02, 6, 6),
  check.names = FALSE
)