在R数据框中按月和年排序

时间:2016-08-10 19:55:55

标签: r date

我按日期清理并订购了我的数据,如下所示:

df1 <- data.frame(matrix(vector(),ncol=4, nrow = 3))
colnames(df1) <- c("Date","A","B","C")
df1[1,] <- c("2000-01-30","0","1","0")
df1[2,] <- c("2000-01-31","2","0","3")
df1[3,] <- c("2000-02-29","1","2","1")
df1[4,] <- c("2000-03-31","2","1","3")
df1
        Date  A  B  C
1 2000-01-30  0  1  0
2 2000-01-31  2  0  3
3 2000-02-29  1  2  1
4 2000-03-31  2  1  3

但是,我想放弃这一天并按月和年订购数据,以便数据看起来像:

        Date  A  B  C
1    2000-01  2  1  3
3    2000-02  1  2  1
4    2000-03  2  1  3

我尝试使用as.yearmon zoo中的df2 <- as.yearmon(df1$Date, "%b-%y"),然后返回NA。提前感谢您的慷慨帮助!

2 个答案:

答案 0 :(得分:3)

这是获取每个月组合中每列的值总和的方法:

library(zoo)
library(dplyr)

# Convert non-date columns to numeric
df1[,-1] = lapply(df1[,-1], as.numeric)

df1 %>% mutate(Date = as.yearmon(Date)) %>%
  group_by(Date) %>%
  summarise_each(funs(sum))

或者,甚至更短:

df1 %>% 
  group_by(Date=as.yearmon(Date)) %>%
  summarise_each(funs(sum))
           Date     A     B     C
1      Jan 2000     2     1     3
2      Feb 2000     1     2     1
3      Mar 2000     2     1     3

其他一些增强功能:

  1. 为每个组添加行数:

    df1 %>% group_by(Date=as.yearmon(Date)) %>%
      summarise_each(funs(sum)) %>%
      bind_cols(df1 %>% count(d=as.yearmon(Date)) %>% select(-d))
    
  2. 多个摘要功能:

    df1 %>% group_by(Date=as.yearmon(Date)) %>%
      summarise_each(funs(sum(.), mean(.))) %>%
      bind_cols(df1 %>% count(d=as.yearmon(Date)) %>% select(-d))
    
  3.            Date A_sum B_sum C_sum A_mean B_mean C_mean     n
    1      Jan 2000     2     1     3      1    0.5    1.5     2
    2      Feb 2000     1     2     1      1    2.0    1.0     1
    3      Mar 2000     2     1     3      2    1.0    3.0     1
    

答案 1 :(得分:0)

Date列需要是日期类型向量时,它是一个字符向量。所以:

df1$Date <- as.Date(df1$Date)
df1$Date <- as.yearmon(df1$Date)

      Date A B C
1 Jan 2000 0 1 0
2 Jan 2000 2 0 3
3 Feb 2000 1 2 1
4 Mar 2000 2 1 3