在从每周汇总到每月汇总后,将日期列添加到数据框

时间:2017-08-09 15:55:14

标签: r

我有以下每周数据框:

onSignup(form:NgForm){
  const email=form.value.email;
  const password=form.value.password;
  this.authService.signupUser(email,password);
  }

然后我使用聚合函数将我的df转换为月度数据帧。

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_monthly中添加一个日期列,显示我将df_monthly写入csv文件之前的月份和年份。我尝试了几种不同的方法,但没有用。

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

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分: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