2017-08-09 119 views
0

后,我有以下的每周数据帧:添加日期列的数据帧从每周汇总每月

df <- data.frame(Date = c("2017-08-01","2017-08-08","2017-08-15", "2017-08-22", "2017-08-29", "2017-09-05"), item1 = c(1.6,1.8,1.6, 2.0, 1.4, 1.5), item2 = c(38.6,35.1,42.6, 43.1, 42, 41), item3 = c(16.9, 17.6, 18.5, 19.8, 17, 18)) 

> df 
     Date item1 item2 item3 
1 2017-08-01 1.6 38.6 16.9 
2 2017-08-08 1.8 35.1 17.6 
3 2017-08-15 1.6 42.6 18.5 
4 2017-08-22 2.0 43.1 19.8 
5 2017-08-29 1.4 42.0 17.0 
6 2017-09-05 1.5 41.0 18.0 

然后我就用DF聚合函数转换为每月数据帧。

df_monthly <- round(aggregate(zoo(df[,-1], as.Date(df$Date)), by=month, FUN=sum),0) 
> df_monthly 
      item1 item2 item3 
    2017.08  8 201 90 
    2017.09  2 41 18 

不,我需要添加一个日期列到df_monthly显示月份和前年我写的df_monthly到CSV文件中。我尝试了几种不同的方法,但没有奏效。

df_monthly$Date<-data.frame(seq(as.Date("2017/08/01"), as.Date("2017/09/05"), "months")) 
> df_monthly 
    item1 item2 item3 Date  
1 8  201 90 Numeric,2 
6 2  41 18 Numeric,2 

任何帮助将不胜感激。

回答

0
library(dplyr) 
library(lubridate) 

df <- data.frame(Date = c("2017-08-01","2017-08-08","2017-08-15", "2017-08-22", "2017-08-29", "2017-09-05"), item1 = c(1.6,1.8,1.6, 2.0, 1.4, 1.5), item2 = c(38.6,35.1,42.6, 43.1, 42, 41), item3 = c(16.9, 17.6, 18.5, 19.8, 17, 18)) 

df %>% 
    mutate(year = year(Date),   # get the year as a variable 
     month = month(Date)) %>%  # get the month as a variable 
    group_by(year, month) %>%   # group by year and month 
    summarise(item1 = sum(item1),  # get the sums for each item 
      item2 = sum(item2), 
      item3 = sum(item3)) %>% 
    ungroup()       # forget the grouping 

# # A tibble: 2 x 5 
# year month item1 item2 item3 
# <dbl> <dbl> <dbl> <dbl> <dbl> 
# 1 2017  8 8.4 201.4 89.8 
# 2 2017  9 1.5 41.0 18.0 

如果你有3分以上的项目为列,你想要一个更通用的解决方案,你可以使用这个

library(dplyr) 
library(lubridate) 
library(tidyr) 

df <- data.frame(Date = c("2017-08-01","2017-08-08","2017-08-15", "2017-08-22", "2017-08-29", "2017-09-05"), item1 = c(1.6,1.8,1.6, 2.0, 1.4, 1.5), item2 = c(38.6,35.1,42.6, 43.1, 42, 41), item3 = c(16.9, 17.6, 18.5, 19.8, 17, 18)) 

df %>% 
    gather(item, value, -Date) %>%  # reshape dataset 
    mutate(year = year(Date),   # get the year as a variable 
     month = month(Date)) %>%  # get the month as a variable 
    group_by(year, month, item) %>%  # group by year, month and item 
    summarise(value = sum(value)) %>% # get the sum of the values 
    spread(item, value) %>%    # reshape dataset back to initial form 
    ungroup()       # forget the grouping 

# # A tibble: 2 x 5 
# year month item1 item2 item3 
# * <dbl> <dbl> <dbl> <dbl> <dbl> 
# 1 2017  8 8.4 201.4 89.8 
# 2 2017  9 1.5 41.0 18.0 
+0

以上解决方案为一年一列,一个用于月份,而不是一个结合了两者。另外,我不会把你的数额整理一下。如果你想让我改变,让我知道,我会更新我的答案。 – AntoniosK

相关问题