【问题标题】:R–Apply function to specific column in listR–将函数应用于列表中的特定列
【发布时间】:2021-01-18 04:32:45
【问题描述】:

我有一个数据框df,看起来像这样:ID 是某只股票,value 是股票价格的虚拟变量,year 是我计算出来的,作为拆分数据的辅助列以下:

id      date value   year
1 2020-12-30    11   2020
1 2020-12-09    12   2020
1 2020-08-01    13   2020 
1 2019-12-30    14   2019
1 2019-12-09    15   2019
1 2019-08-01    16   2019
2 2020-12-30    17   2020
2 2020-12-09    18   2020
2 2020-08-01    19   2020
2 2019-12-29    20   2019
2 2019-12-09    21   2019
2 2019-08-01    22   2019

我想为每年的每个 id 查找我有数据的最后一天是什么。通常,这是年末,但在我的大型数据集中并非总是如此,所以我不想硬编码年末。

我已经根据 id 和年份将其拆分为一个列表,代码和结果如下:

list <- split(df, list(df$id, df$year))

现在,在列表的 4 个元素中的每一个中,我想创建一个新列,以提供相应列表中日期列的最大值。例如。我希望第一个列表元素有以下输出:

id      date value   year    maxdate
1 2020-12-30    11   2020 2020-12-30
1 2020-12-09    12   2020 2020-12-30
1 2020-08-01    13   2020 2020-12-30

你能帮我实现想要的输出吗?

我曾尝试使用某些版本的 apply 系列函数,但仅根据我的每个列表元素中的日期列无法使其工作。

非常感谢您!

最好的问候, C

【问题讨论】:

    标签: r


    【解决方案1】:

    我们使用lapply 循环遍历listtransform 以创建'maxdate'

    list1 <- lapply(list1, transform, maxdate = max(date))
    

    假设“日期”是Date


    或者使用tidyverse

    library(dplyr)
    library(purrr)
    list1 <- map(list1, ~ .x %>%
                             mutate(maxdate = max(date)))
    

    如果我们使用分组操作也可以简化而不拆分

    df %>%
        group_by(id, year) %>%
        mutate(maxdate = max(date))
    

    在哪里

    list1 <- split(df, list(df$id, df$year), drop = TRUE)
    

    注意:最好不要用函数名称命名对象,例如list

    【讨论】:

      【解决方案2】:

      ave 中使用自定义的tail

      f <- function(x) tail(x, 1)
      d <- transform(d, last=ave(date, id, year, FUN=f))
      d
      #    id       date value year       last
      # 1   1 2020-12-30    11 2020 2020-08-01
      # 2   1 2020-12-09    12 2020 2020-08-01
      # 3   1 2020-08-01    13 2020 2020-08-01
      # 4   1 2019-12-30    14 2019 2019-08-02
      # 5   1 2019-12-09    15 2019 2019-08-02
      # 6   1 2019-08-02    16 2019 2019-08-02
      # 7   2 2020-12-30    17 2020 2020-08-03
      # 8   2 2020-12-09    18 2020 2020-08-03
      # 9   2 2020-08-03    19 2020 2020-08-03
      # 10  2 2019-12-29    20 2019 2019-08-04
      # 11  2 2019-12-09    21 2019 2019-08-04
      # 12  2 2019-08-04    22 2019 2019-08-04
      

      请注意,我稍微更改了您的数据以使效果可见:)


      数据:

      d <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
      2L, 2L), date = structure(c(18626, 18605, 18475, 18260, 18239, 
      18110, 18626, 18605, 18477, 18259, 18239, 18112), class = "Date"), 
          value = 11:22, year = c(2020L, 2020L, 2020L, 2019L, 2019L, 
          2019L, 2020L, 2020L, 2020L, 2019L, 2019L, 2019L)), row.names = c(NA, 
      -12L), class = "data.frame")
      

      【讨论】:

        【解决方案3】:

        data.table 选项通过添加列 last

        setDT(df)[, last := max(date), .(id, year)]
        

        给予

        > df
            id       date value year       last
         1:  1 2020-12-30    11 2020 2020-12-30
         2:  1 2020-12-09    12 2020 2020-12-30
         3:  1 2020-08-01    13 2020 2020-12-30
         4:  1 2019-12-30    14 2019 2019-12-30
         5:  1 2019-12-09    15 2019 2019-12-30
         6:  1 2019-08-02    16 2019 2019-12-30
         7:  2 2020-12-30    17 2020 2020-12-30
         8:  2 2020-12-09    18 2020 2020-12-30
         9:  2 2020-08-03    19 2020 2020-12-30
        10:  2 2019-12-29    20 2019 2019-12-29
        11:  2 2019-12-09    21 2019 2019-12-29
        12:  2 2019-08-04    22 2019 2019-12-29
        

        数据

        > dput(df)
        structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
        2L, 2L), date = structure(c(18626, 18605, 18475, 18260, 18239,
        18110, 18626, 18605, 18477, 18259, 18239, 18112), class = "Date"),
            value = 11:22, year = c(2020L, 2020L, 2020L, 2019L, 2019L,
            2019L, 2020L, 2020L, 2020L, 2019L, 2019L, 2019L)), row.names = c(NA,
        -12L), class = "data.frame")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 2020-05-24
          • 2014-03-17
          • 1970-01-01
          • 2023-03-17
          • 2015-05-08
          • 1970-01-01
          相关资源
          最近更新 更多