Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

Cut and aggregate multiple columns

I have some data that looks-like this:

id1         id2         id3         id4        amt
2021-06-15  2021-06-30  2021-07-15  2021-07-30  10
2021-06-15  2021-06-30  2021-07-15  2021-07-30  20
2021-07-15  2021-06-30  2021-07-15  2021-07-30  30
2021-07-30  2021-06-30  2021-07-15  2021-07-30  10

and I create a new column with

dat$month <- as.Date(cut(dat$id1, breaks="month"))

in order to sum the amounts by month so that I can cbind the amount like this:

bymonth <- aggregate(cbind(amt)~month, data=dat,FUN=sum)

I want to sum by month for each id#, and then cbind an aggregate ~month for each id#. Do I need to create a month column for each id - like month-id1, monthid2, etc., and then cbind a bymonth-id1m, bymonth-id2, etc.? I eventually create a time series for each id#, so I think it would work to create these one-by-one, but it doesn't feel very elegant.

question from:https://stackoverflow.com/questions/66056120/cut-and-aggregate-multiple-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...