如何在 r 中对相同的年份而不是相同的月份进行分组

how to group same years instead of same month in r

我的数据在相应月份的名称下以月年格式分组。但是我想把他们归到对应的年份下。

我有一个如下列表:值按相同的月份分组。

$Apr
$Apr$`04-2036`
          date value
116 04-25-2036  1.14
117 04-26-2036  0.67

$Apr$`04-2037`
          date value
478 04-22-2037     0
479 04-23-2037     0


$Mar
$Mar$`03-2037`
          date value
446 03-21-2037  1.67
447 03-22-2037  0.00


$May
$May$`05-2036`
          date value
146 05-25-2036  0.00
147 05-26-2036  2.31

这是它的结构:

sample<-structure(list(Apr = structure(list(`04-2036` = structure(list(
    date = c("04-25-2036", "04-26-2036"), value = c(1.14, 0.67
    )), .Names = c("date", "value"), row.names = 116:117, class = "data.frame"), 
    `04-2037` = structure(list(date = c("04-22-2037", "04-23-2037"
    ), value = c(0, 0)), .Names = c("date", "value"), row.names = 478:479, class = "data.frame")), .Names = c("04-2036", 
"04-2037")), Mar = structure(list(`03-2037` = structure(list(
    date = c("03-21-2037", "03-22-2037"), value = c(1.67, 0)), .Names = c("date", 
"value"), row.names = 446:447, class = "data.frame")), .Names = "03-2037"), 
    May = structure(list(`05-2036` = structure(list(date = c("05-25-2036", 
    "05-26-2036"), value = c(0, 2.31)), .Names = c("date", "value"
    ), row.names = 146:147, class = "data.frame")), .Names = "05-2036")), .Names = c("Apr", 
"Mar", "May"))

期望的输出:数据将按相同年份分组。

$`2036`
$`2036`$`04-2036`
          date value
116 04-25-2036  1.14
117 04-26-2036  0.67

$`2036`$`05-2036`
          date value
146 05-25-2036  0.00
147 05-26-2036  2.31


$`2037`
$`2037`$`03-2037`
          date value
446 03-21-2037  1.67
447 03-22-2037  0.00

$`2037`$`04-2037`
          date value
478 04-22-2037     0
479 04-23-2037     0

输出结构如下所示:

output<-structure(list(`2036` = structure(list(`04-2036` = structure(list(
    date = c("04-25-2036", "04-26-2036"), value = c(1.14, 0.67
    )), .Names = c("date", "value"), row.names = 116:117, class = "data.frame"), 
    `05-2036` = structure(list(date = c("05-25-2036", "05-26-2036"
    ), value = c(0, 2.31)), .Names = c("date", "value"), row.names = 146:147, class = "data.frame")), .Names = c("04-2036", 
"05-2036")), `2037` = structure(list(`03-2037` = structure(list(
    date = c("03-21-2037", "03-22-2037"), value = c(1.67, 0)), .Names = c("date", 
"value"), row.names = 446:447, class = "data.frame"), `04-2037` = structure(list(
    date = c("04-22-2037", "04-23-2037"), value = c(0, 0)), .Names = c("date", 
"value"), row.names = 478:479, class = "data.frame")), .Names = c("03-2037", 
"04-2037"))), .Names = c("2036", "2037"))

您可以将数据组合成一个数据框,从中提取年份并split

library(dplyr)
library(purrr)

map_df(sample, bind_rows, .id = 'month') %>% 
  mutate(date = mdy(date), 
         year = year(date)) %>%
  split(.$year) %>%
  map(~split(.x, .x$month))

#$`2036`
#$`2036`$Apr
#  month       date value year
#1   Apr 2036-04-25  1.14 2036
#2   Apr 2036-04-26  0.67 2036

#$`2036`$May
#  month       date value year
#7   May 2036-05-25  0.00 2036
#8   May 2036-05-26  2.31 2036


#$`2037`
#$`2037`$Apr
#  month       date value year
#3   Apr 2037-04-22     0 2037
#4   Apr 2037-04-23     0 2037

#$`2037`$Mar
#  month       date value year
#5   Mar 2037-03-21  1.67 2037
#6   Mar 2037-03-22  0.00 2037

我们可以使用base R

df1 <- transform(do.call(rbind, Map(cbind, Month = names(sample), 
     lapply(sample, function(x) do.call(rbind, x)))), date = as.Date(date, "%m-%d-%Y")) 
df1$year <- format(df1$date, "%Y")
row.names(df1) <- NULL
lapply(split(df1, df1$year), function(x) split(x, x$Month))
$`2036`
$`2036`$Apr
  Month       date value year
1   Apr 2036-04-25  1.14 2036
2   Apr 2036-04-26  0.67 2036

$`2036`$May
  Month       date value year
7   May 2036-05-25  0.00 2036
8   May 2036-05-26  2.31 2036


$`2037`
$`2037`$Apr
  Month       date value year
3   Apr 2037-04-22     0 2037
4   Apr 2037-04-23     0 2037

$`2037`$Mar
  Month       date value year
5   Mar 2037-03-21  1.67 2037
6   Mar 2037-03-22  0.00 2037