我有一个库存数据的大型数据文件。
我已经通过自动收报机和数据组织了它:
Ticker Date Vol.
A 20050501 23
A 20050502 45
AB 20050501 100
AV 20050503 100
BC 20050501 50
如何在保持相同的字母和日期格式的同时,将数据分组并汇总为每周音量?例如,将23和45相加并输入如下所示的条目:
A 20050501 68
AB 20050501 100
我想列出A的每周总量,然后列出AB的所有每周总和等。
谢谢。
答案 0 :(得分:1)
以下是使用lubridate
和dplyr
的解决方案:
require(lubridate)
require(dplyr)
set.seed(1)
Ticker<-rep(c("a","b","c"),6)
Vol.<-rnorm(18,100,100)
Date<-c("20050501","20050502","20050503","20050504","20050505","20050506","20050507","20050508","20050509",
"20050510","20050511","20050512","20050513","20050514","20050515","20050516","20050517","20050518")
dat<-data.frame(Ticker,Date,Vol.)
dat$Date<-ymd(dat$Date)
dat$Week<-week(dat$Date)
dat_summary<-dat%>%
group_by(Week,Ticker)%>%
summarize(sumweek=sum(Vol.))
dat_summary
#Source: local data frame [9 x 3]
#Groups: Week
# Week Ticker sumweek
#1 18 a 296.88270
#2 18 b 251.31511
#3 18 c 16.43714
#4 19 a 218.20407
#5 19 b 425.01059
#6 19 c 314.51562
#7 20 a 133.38258
#8 20 b -23.08902
#9 20 c 406.87671
如果您的数据有一年以上,那么您需要创建“年度列”dat$Year<-year(dat$Date)
,然后按“周和年”group_by(Week,Year,Ticker)
进行分组。