如何在 R 中仅选择每周的最后一小时天气数据?

How to select only the last hour of weather data from each week in R?

提问人:Ahsk 提问时间:7/29/2023 更新时间:7/30/2023 访问量:37

问:

我有一个天气数据集,每隔 15 分钟收集一次观测数据,持续数周。我只想提取天气数据,而忽略其余的。last houreach week

enter image description here

例如,在一周中,我只想保留单元格 147 到 150 的行(最后四行 - 每行代表 15 分钟:4*15 = 1 小时)并忽略其余的天气数据。15

可重复的示例

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 DataFrame DPLYR 操作 数据 整理

评论


答:

2赞 r2evans 7/29/2023 #1

由于您没有“时间”变量,因此您要求提供“每周的最后 4 行”,在这种情况下:

library(dplyr)
slice_tail(df, n=4, by=week)
# # A tibble: 104 × 2
#    week  precipitation
#    <fct>         <dbl>
#  1 1                 0
#  2 1                 0
#  3 1                 0
#  4 1                 0
#  5 2                 0
#  6 2                 0
#  7 2                 0
#  8 2                 0
#  9 3                 0
# 10 3                 0
# # ℹ 94 more rows
# # ℹ Use `print(n = ...)` to see more rows
1赞 AndS. 7/30/2023 #2

下面是一个基本的 R 选项:

split(df, df$week) |>
  Map(f = \(x) tail(x, 4)) |>
  do.call(what = rbind.data.frame)