在数据框列表中提取季节

时间:2019-11-05 15:14:55

标签: r list dataframe extract lubridate

我有一个数据帧列表,例如

library(lubridate)
df1 = data.frame (Date = seq(ymd('2005-04-16'),ymd('2016-10-15'),by='day'), 
                  Station1423 = replicate(1,sample(0:130, 4201,rep=TRUE)))
df2 = data.frame (Date = seq(ymd('2001-09-02'),ymd('2018-12-31'),by='day'), 
                  Station4322 = replicate(1,sample(0:130, 6330,rep=TRUE)))
df3 = data.frame (Date = seq(ymd('1995-02-20'),ymd('2018-12-31'),by='day'), 
                  Station1242 = replicate(1,sample(0:130, 8716,rep=TRUE)))

dflist = list(df1, df2, df3)
names(dflist) = c("df1", "df2", "df3")

我现在想为列表中的每个数据框提取季节。结果应该是这样的嵌套列表:

dflist    list of 3
   df1       list of 4
     DJF: dataframe
     ..$Date
     ..$Station1423   
     JJA: dataframe
     ..$Date
     ..$Station1423   
     MAM: dataframe
     ..$Date
     ..$Station1423 
     SON: dataframe
     ..$Date
     ..$Station1423     
 df2       list of 4
     DJF: dataframe
     ..$Date
     ..$Station4322   
     JJA: dataframe
     ..$Date
     ..$Station4322   
.....and so on

我也可以接受这样的简单列表:

dflist    list of 12
  df1_DJF: dataframe
      ..$Date
      ..$Station1423 
  df1_JJA: dataframe
......

1 个答案:

答案 0 :(得分:2)

建立一个编码季节到一个月的数据框。

seasons <- tibble(month = 1:12, season = c(rep("DJF", 2), rep("MAM", 3), rep("JJA", 3), rep("SON", 3), "DJF"))

使用map按月加入每个数据帧,然后按季节加入split

library(lubridate)
library(purrr)
library(dplyr)

new_list <- dflist %>%
  map( ~ {mutate(., month = month(Date)) %>%
      left_join(seasons, by = "month") %>%
      split(f = .$season)
  })

请注意,这会将两列添加到原始数据帧。我们可以使用map再次轻松删除它们。

new_list %>%
  map_depth(.depth = 2, ~ select(., -month, - season))

或在原件中嵌套另一个map

new_list <- dflist %>%
  map( ~ {mutate(., month = month(Date)) %>%
      left_join(seasons, by = "month") %>%
      split(f = .$season) %>%
      map(~ select(., -month, -season))
  })