【问题标题】:dplyr: Filter only if multiple conditions in different columns are metdplyr:仅在满足不同列中的多个条件时过滤
【发布时间】:2018-02-19 23:02:57
【问题描述】:

这是我的 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
)

我想排除“my_video_item_duration_min”值小于 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 且 ratiofuturepast 不同于 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
    )

【问题讨论】:

  • futuremw:1 不是合法的变量名:冒号表明这是一个序列。你的意思是`futuremw:1`(注意额外的反引号)?
  • 请插入您的数据样本,而不是您的数据的图像,这样可以使这个问题更具重现性。有good examples如何做到这一点,方便消费。 (此外,您的图像包含变量名称`pastmw:1`,但您的代码显示为pastmw_1 == 3,请在您拥有的和使用的代码方面保持一致。)
  • 所以你的预期结果数据集是空的? “持续时间”过滤器保持 4-5。参与者 5 可能会离开,因为你没有说如何处理 NA。由于您的其他规则,4 被排除在外。
  • 不是全部。参与者 1 不应被排除,因为他的持续时间过滤器为 5,而不是例如 4.9。参与者 5 不应被排除,因为 NA 我想包括那些提出 NA 的人。
  • 您使用&gt; 5,不包括参与者1。

标签: r dplyr filtering


【解决方案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
)

【讨论】:

  • 也许值得解释一下:x|y|z 可能评估为 NA,在这种情况下 filter 将其视为 FALSE,例如 mtcars %&gt;% slice(1:3) %&gt;% filter(c(TRUE, FALSE, NA))
  • 它不起作用,因为没有参与者被包括在内。这是输出:[1] Participant futuremw:1 pastmw:1 [4] ratiofuturepast:1 my_video_item_duration_min (或0-length row.names)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
相关资源
最近更新 更多