簡體   English   中英

使用R語言按月分別分組

[英]separate grouping by months using R-Language

使用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

好的,我看到您實際上想按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
> 

也許這使用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

使用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

如果不確定要如何匯總日期,請首先使用周,月和年以及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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM