Dear R Experts, I have data in the following format x1 x2 time 2 4 1 3 1 2 4 6 3 1 4 4 5 6 5 8 5 6 . . . . . . . . . 1 5 399 3 4 400 Time represents each month which simply has a number from 1 to 400 (i.e. the data covers 400 months). I would like to somehow create two new variables which counts x1 and x2 as quarterly data in stead of monthly. Like this: x1 x1 Quarter 10 12 1 12 15 2 . . . . . . . . . 23 21 100 Can any one tell me how this can be done in R? With kind regards, Thomas Jensen
One approach: d <- data.frame(x1=c(2,3,4,1,5,8), x2=c(4,1,6,4,6,5), time=1:6) d$quarter <- (d$time-1) %/% 4 # Or whatever your logic is aggregate(cbind(x1,x2) ~ quarter, data = d, sum) # quarter x1 x2 # 1 0 10 15 # 2 1 13 11 Hope this helps Allan On 28/06/10 13:23, Thomas Jensen wrote:> Dear R Experts, > > I have data in the following format > > x1 x2 time > 2 4 1 > 3 1 2 > 4 6 3 > 1 4 4 > 5 6 5 > 8 5 6 > . . . > . . . > . . . > 1 5 399 > 3 4 400 > > Time represents each month which simply has a number from 1 to 400 > (i.e. the data covers 400 months). I would like to somehow create two > new variables which counts x1 and x2 as quarterly data in stead of > monthly. Like this: > > x1 x1 Quarter > 10 12 1 > 12 15 2 > . . . > . . . > . . . > 23 21 100 > > Can any one tell me how this can be done in R? > > With kind regards, > Thomas Jensen > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Thanks, Allan, that did the trick :) Best, Thomas On Jun 28, 2010, at 6:13 PM, Allan Engelhardt wrote:> One approach: > > d <- data.frame(x1=c(2,3,4,1,5,8), x2=c(4,1,6,4,6,5), time=1:6) > d$quarter <- (d$time-1) %/% 4 # Or whatever your logic is > aggregate(cbind(x1,x2) ~ quarter, data = d, sum) > # quarter x1 x2 > # 1 0 10 15 > # 2 1 13 11 > > > Hope this helps > > Allan > > > On 28/06/10 13:23, Thomas Jensen wrote: >> Dear R Experts, >> >> I have data in the following format >> >> x1 x2 time >> 2 4 1 >> 3 1 2 >> 4 6 3 >> 1 4 4 >> 5 6 5 >> 8 5 6 >> . . . >> . . . >> . . . >> 1 5 399 >> 3 4 400 >> >> Time represents each month which simply has a number from 1 to 400 >> (i.e. the data covers 400 months). I would like to somehow create >> two new variables which counts x1 and x2 as quarterly data in stead >> of monthly. Like this: >> >> x1 x1 Quarter >> 10 12 1 >> 12 15 2 >> . . . >> . . . >> . . . >> 23 21 100 >> >> Can any one tell me how this can be done in R? >> >> With kind regards, >> Thomas Jensen >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code.