简体   繁体   English

在 r 中按月/年求和变量

[英]Sum variable by month/year in r

I receive historical demand data from an excel spreadsheet in the following format:我从 Excel 电子表格中收到以下格式的历史需求数据:

Part Number  Requested Date       Quantity
123           01/24/2013 12:53       1
122           02/07/2013 09:57       1 
122           02/14/2013 09:58       7
124           11/21/2012 12:46       1

I typically provide my management charts from excel by part like this but I want to be more proficient in R---doing hundreds of these at time我通常像这样按部分提供我的 excel 管理图表,但我想更精通 R ---一次做数百个

We can try with as.yearmon to create the grouping variable and then get the sum of 'Quantity' in summarise我们可以尝试使用as.yearmon创建分组变量,然后在summarise获取“数量”的sum

library(zoo)
library(dplyr)
df1 %>% 
   group_by(PartNumber, yearMon = as.yearmon(RequestedDate, "%m/%d/%Y %H:%M")) %>% 
   summarise(Quantity = sum(Quantity))

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

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