如何根据R中的条件选择某个时期?

How to select a certain period based on a condition in R?

提问人:Ahsk 提问时间:4/11/2023 更新时间:4/11/2023 访问量:42

问:

我进行了几次实验,持续了大约一周。对于每周,我希望保留降水开始到周末的周期/行。

我知道如何摒弃干燥的几周,但我不确定如何选择从下雨开始到潮湿周结束的时期。

我的数据如下所示

enter image description here

在 中,我只想保留单元格 144 到 150 中的行并丢弃其余的行。完整的可以丢弃,因为这是一个完全干燥的一周。week 15week 16

注意:如果降水开始时有多个干/湿时段,直到周末,这很好。关键是忽略每周降雨发生前的干旱期。感谢您的帮助。

这是一个可重现的例子

df <- structure(
  list(
    week = structure(
      c(
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        13L,
        13L,
        13L,
        13L,
        13L,
        13L,
        13L,
        13L,
        13L,
        13L,
        14L,
        14L,
        14L,
        14L,
        14L,
        14L,
        14L,
        14L,
        14L,
        14L,
        15L,
        15L,
        15L,
        15L,
        15L,
        15L,
        15L,
        15L,
        15L,
        15L,
        16L,
        16L,
        16L,
        16L,
        16L,
        16L,
        16L,
        16L,
        16L,
        16L,
        17L,
        17L,
        17L,
        17L,
        17L,
        17L,
        17L,
        17L,
        17L,
        17L,
        18L,
        18L,
        18L,
        18L,
        18L,
        18L,
        18L,
        18L,
        18L,
        18L,
        19L,
        19L,
        19L,
        19L,
        19L,
        19L,
        19L,
        19L,
        19L,
        19L,
        20L,
        20L,
        20L,
        20L,
        20L,
        20L,
        20L,
        20L,
        20L,
        20L,
        21L,
        21L,
        21L,
        21L,
        21L,
        21L,
        21L,
        21L,
        21L,
        21L,
        22L,
        22L,
        22L,
        22L,
        22L,
        22L,
        22L,
        22L,
        22L,
        22L,
        23L,
        23L,
        23L,
        23L,
        23L,
        23L,
        23L,
        23L,
        23L,
        23L,
        24L,
        24L,
        24L,
        24L,
        24L,
        24L,
        24L,
        24L,
        24L,
        24L,
        25L,
        25L,
        25L,
        25L,
        25L,
        25L,
        25L,
        25L,
        25L,
        25L,
        26L,
        26L,
        26L,
        26L,
        26L,
        26L,
        26L,
        26L,
        26L,
        26L
      ),
      .Label = c(
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23",
        "24",
        "25",
        "26"
      ),
      class = "factor"
    ),
    precipitation = c(
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.59999999999997,
      0.800000000000011,
      0.600000000000023,
      0.199999999999989,
      0.399999999999977,
      0.400000000000034,
      1,
      0.799999999999955,
      0.400000000000034,
      0.800000000000011,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      9.60000000000002,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0.200000000000045,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0.200000002980232,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    )
  ),
  class = c("tbl_df",
            "tbl", "data.frame"),
  row.names = c(NA,-260L)
)
r dplyr 操作 挖掘 挖掘 数据

评论


答:

2赞 r2evans 4/11/2023 #1

我们可以为此使用:filter(cumany(.))

library(dplyr)
out <- df %>%
  group_by(week) %>%
  filter(cumany(precipitation > 0)) %>%
  ungroup()
filter(out, week == "15")
# # A tibble: 7 × 2
#   week  precipitation
#   <fct>         <dbl>
# 1 15             9.60
# 2 15             0   
# 3 15             0   
# 4 15             0   
# 5 15             0   
# 6 15             0   
# 7 15             0   

评论

1赞 Konrad Rudolph 4/11/2023
很好,我本来打算用的,但这有点好。cumsum(precipitation) > 0
0赞 r2evans 4/11/2023
那是我答案的第一次迭代,直到我想起来:-)......我之前已经包含在 base-R 答案中(不知道),经常从某人那里得到一个提示,所以显然我对累积逻辑函数有一些内部包混淆cumanycumanydplyr::cumany