Re: [R] sum part of a vector

2011-07-23 Thread peter dalgaard
On Jul 23, 2011, at 13:11 , Simon Kiss wrote: > Dear colleagues, I have a data set that looks roughly like this; > mydat<-data.frame(state=c(rep("Alabama", 5), rep("Delaware", 5), > rep("California", 5)), news=runif(15, min=0, max=8), cum.news=rep(0, 15)) > > For each state, I'd like to cumulat

Re: [R] sum part of a vector

2011-07-23 Thread Weidong Gu
Hi Simon, Is this what you want? mydat$cum.news<-unlist(tapply(mydat$news,mydat$state,FUN=cumsum)) Weidong Gu On Sat, Jul 23, 2011 at 7:11 AM, Simon Kiss wrote: > Dear colleagues, I have a data set that looks roughly like this; > mydat<-data.frame(state=c(rep("Alabama", 5), rep("Delaware", 5),

[R] sum part of a vector

2011-07-23 Thread Simon Kiss
Dear colleagues, I have a data set that looks roughly like this; mydat<-data.frame(state=c(rep("Alabama", 5), rep("Delaware", 5), rep("California", 5)), news=runif(15, min=0, max=8), cum.news=rep(0, 15)) For each state, I'd like to cumulatively sum the value of "news" and make that put that valu