简体   繁体   English

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

[英]Order by month and year in R data frame

I have cleaned and ordered my data by date, which looks like below: 我按日期清理并订购了我的数据,如下所示:

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

However, I want to drop the day and order the data by month and year so the data will look like: 但是,我想删除当天按月和年订购数据,以便数据如下所示:

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

I tried to use as.yearmon from zoo df2 <- as.yearmon(df1$Date, "%b-%y") and it returns NA . 我试图从zoo df2 <- as.yearmon(df1$Date, "%b-%y")使用as.yearmon并返回NA Thank you in advance for your generous help! 提前感谢您的慷慨帮助!

Here's a way to get the sum of the values for each column within each combination of Year-Month: 这是一种获取Year-Month的每个组合中每列的值总和的方法:

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))

Or, even shorter: 或者,甚至更短:

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

A couple of additional enhancements: 其他一些增强功能:

  1. Add the number of rows for each group: 添加每个组的行数:

     df1 %>% group_by(Date=as.yearmon(Date)) %>% summarise_each(funs(sum)) %>% bind_cols(df1 %>% count(d=as.yearmon(Date)) %>% select(-d)) 
  2. Multiple summary functions: 多个汇总功能:

     df1 %>% group_by(Date=as.yearmon(Date)) %>% summarise_each(funs(sum(.), mean(.))) %>% bind_cols(df1 %>% count(d=as.yearmon(Date)) %>% select(-d)) 
  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 

Your Date column is a character vector, when it needs to be a Date type vector. Date列需要是Date类型向量时,它是一个字符向量。 So: 所以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM