使用R语言按月分别分组

时间:2018-09-04 08:38:34

标签: r dplyr

我使用dput提供数据。

df <- 
  structure(list(Goods = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                     1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                     2L, 2L, 2L, 2L, 2L, 2L), 
                                   .Label = c("IceScream", "Kex"), class = "factor"), 
                 date = structure(c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                    3L, 4L, 4L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
                                    3L, 3L, 3L, 3L, 4L, 4L), 
                                  .Label = c("12.01.2015", "13.01.2015", 
                                             "14.01.2015", "15.01.2015"), class = "factor"), 
                 Y = c(200L, 50L, 100L, 50L, 200L, 200L, 50L, 200L, 100L, 1000L, 
                       1000L, 50L, 50L, 100L, 200L, 50L, 23L, 50L, 200L, 200L, 
                       45L, 200L, 100L, 6L, 23L, 50L, 50L, 436L)), 
            .Names = c("Goods", "date", "Y"), class = "data.frame", 
            row.names = c(NA, -28L))

我想按月汇总数据。

library(dplyr)
library(lubridate)
library(zoo)

df <- df %>%
  group_by(yearMon = as.yearmon(dmy(date))) %>% 
  summarise(new = sum(new))

但是我有一组商品(冰淇淋和凯克斯)。 每个组如何按月汇总?

类似这样的东西

Goods        date         Y
IceScream   jan-2015    3350
Kex         jan-2015    1633

4 个答案:

答案 0 :(得分:2)

好的,我看到您实际上想按Y的数量来总结,这很糟糕:

df2<-df %>% group_by( yearMon = as.yearmon(dmy(date)), Goods ) %>% 
summarise(new = sum(Y))

df2

# A tibble: 2 x 3
# Groups:   yearMon [?]
yearMon       Goods       new
<S3: yearmon> <fct>     <int>
1 Jan 2015      IceScream  3350
2 Jan 2015      Kex        1633

如果要按周汇总,请使用:

df2 <- df %>% group_by(Goods,week = week(dmy(date)), ) %>% summarise(new =sum(Y))

给出:

> df2
# A tibble: 4 x 3
# Groups:   Goods [?]
Goods      week   new
<fct>     <dbl> <int>
1 IceScream     2  3200
2 IceScream     3   150
3 Kex           2  1147
4 Kex           3   486
> 

答案 1 :(得分:1)

也许使用base函数而不是其他软件包(dplyr除外):

df %>%
  group_by(Goods, yearMon = format(as.Date(date,'%d.%m.%Y'),'%b-%Y')) %>% 
  summarise(new = sum(Y))
# A tibble: 2 x 3
# Groups:   Goods [?]
  Goods     yearMon    new
  <fct>     <chr>    <int>
1 IceScream jan-2015  3350
2 Kex       jan-2015  1633

编辑
更基本:

df$yearMon <- format(as.Date(df$date,'%d.%m.%Y'),'%b-%Y')
aggregate(cbind(Y) ~ Goods + yearMon, data = df, sum, na.rm = TRUE)

      Goods  yearMon    Y
1 IceScream gen-2015 3350
2       Kex gen-2015 1633

编辑2:
要以每周的1月1日为准,可以尝试以下方法:

 library(lubridate)
df %>%
  group_by(Goods, week =paste(ceiling(day(as.Date(date,'%d.%m.%Y')) / 7),month(as.Date(date,'%d.%m.%Y'), label = TRUE, abbr = TRUE),sep='-')
) %>% 
  summarise(new = sum(Y))

# A tibble: 4 x 3
# Groups:   Goods [?]
  Goods     week    new
  <fct>     <chr> <int>
1 IceScream 2-jan  3200
2 IceScream 3-jan   150
3 Kex       2-jan  1147
4 Kex       3-jan   486

答案 2 :(得分:1)

使用sqldf

df$date= as.POSIXct(df$date, format="%d.%m.%Y") # Convert date to POSIXct
df$date=format(as.Date(df$date), "%Y-%m") # Extract year and month
library(sqldf) # Using sqldf group by date and Goods
sqldf("select Goods,date,sum(Y) from df group by date,Goods")

输出:

   Goods    date      sum(Y)
1 IceScream 2015-01   3350
2       Kex 2015-01   1633

答案 3 :(得分:0)

如果不确定如何汇总日期,请首先使用周,月,年和thenn创建单独的列,以便更轻松地测试不同的版本:

library(dplyr)
library(lubridate)

df <- df %>% 
  mutate(date = dmy(date), 
         week = week(date),
         month = month(date, label = T), 
         year = year(date))

df
#    Goods       date    Y week month year
# 1  IceScream 2015-01-12  200    2   Jan 2015
# 2  IceScream 2015-01-12   50    2   Jan 2015
# 3  IceScream 2015-01-13  100    2   Jan 2015
# 4  IceScream 2015-01-13   50    2   Jan 2015
# 5  IceScream 2015-01-13  200    2   Jan 2015
# 6  IceScream 2015-01-14  200    2   Jan 2015

仅按年份和月份分组:

df %>% 
  group_by(Goods, year, month) %>% 
  summarise(sum_y = sum(Y))

# A tibble: 2 x 4
# Groups:   Goods, year [?]
#   Goods      year month sum_y
#   <fct>     <dbl> <ord> <int>
# 1 IceScream  2015 Jan    3350
# 2 Kex        2015 Jan    1633

按年份月份和星期分组:

df %>% 
  group_by(Goods, year, month, week) %>% 
  summarise(sum_y = sum(Y))

# A tibble: 4 x 5
# Groups:   Goods, year, month [?]
#   Goods      year month  week sum_y
#   <fct>     <dbl> <ord> <dbl> <int>
# 1 IceScream  2015 Jan       2  3200
# 2 IceScream  2015 Jan       3   150
# 3 Kex        2015 Jan       2  1147
# 4 Kex        2015 Jan       3   486