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
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
2 matches
Mail list logo