How to run lm for each subset of the data frame, and then aggreage the result? [duplicate]
问题 This question already has answers here : Linear Regression and group by in R (10 answers) Closed 3 years ago . 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 country. Then aggregate the result into a new data frame looks like : countryname, coeffname 1 USA 1.2 2 GB 1.0 3