Re: [R] looping by grouping variable

2011-08-31 Thread jim holtman
you can use 'ave' to add a new column with the state average: > id<-as.character(c(01001:01010, 02001:02010)) > st<-substr(id,1,1) > cnty<-substr(id,2,5) > tfr10<-rnorm(1:20) > > mydata<-data.frame(id,st,cnty,tfr10) > mydata$stAvg <- ave(mydata$tfr10, mydata$st) > print(mydata) id st cnty

[R] looping by grouping variable

2011-08-31 Thread jour4life
Hello all, I hope something is not already posted regarding this exact problem I am trying to solve. I've read through the forums and previous postings and am still confused as to how to approach this. Basically, what I am trying to do is construct variables that utilizes an average of a variable