[R] How to run lm for each subset of the data frame, and then aggreage the result?

2013-05-19 Thread CHEN, Cheng
Hi gurus, I have a big data frame df, with columns named as : age, income, country what I want to do is very simpe actually, do fitFunc-function(thisCountry){ subframe-df[which(country==thisCountry),]; fit-lm(income~0+age, data=subframe); return(coef(fit));} for each individual

Re: [R] How to run lm for each subset of the data frame, and then aggreage the result?

2013-05-19 Thread arun
HI, May be this helps: set.seed(24) dat1- data.frame(age=sample(30:70,120,replace=TRUE),income=sample(4:8,120,replace=FALSE),country=rep(c(USA,GB,France),each=40),stringsAsFactors=FALSE) library(plyr)  ldply(dlply(dat1,.(country),lm,formula=income~0+age),function(x) coef(x)) #  country   

Re: [R] How to run lm for each subset of the data frame, and then aggreage the result?

2013-05-19 Thread David Winsemius
On May 19, 2013, at 5:31 AM, CHEN, Cheng wrote: Hi gurus, I have a big data frame df, with columns named as : age, income, country what I want to do is very simpe actually, do fitFunc-function(thisCountry){ subframe-df[which(country==thisCountry),]; fit-lm(income~0+age,