【问题标题】:how to group same years instead of same month in r如何在 r 中对同年而不是同月进行分组
【发布时间】:2021-08-15 14:06:44
【问题描述】:

我的数据以月-年格式分组在相应月份的名称下。但我想将它们按相应的年份分组。

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

$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"))

【问题讨论】:

    标签: r grouping


    【解决方案1】:

    您可以将数据合并到一个数据框中,从中提取年份和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
    

    【讨论】:

    • 这看起来不错,但是我需要将我的函数分别应用于每个月。例如,我将该函数应用于 2036 下的一个元素(例如 04-2036)。然后,我将对2037以下的一个元素应用相同的功能(例如03-2037)
    • 也许你可以再拆分一次。如果有帮助,请参阅更新的答案。
    【解决方案2】:

    我们可以使用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
    

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 1970-01-01
      • 2020-03-22
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 2017-01-25
      • 1970-01-01
      相关资源
      最近更新 更多