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

arun smartpink111 at yahoo.com
Sun May 19 18:10:25 CEST 2013


HI,
May be this helps:

set.seed(24)
dat1<- data.frame(age=sample(30:70,120,replace=TRUE),income=sample(40000:80000,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      age
#1  France 1127.192
#2      GB 1194.586
#3     USA 1161.795
#or
do.call(rbind,lapply(split(dat1,dat1$country),function(x) coef(with(x,lm(income~0+age)))))
#            age
#France 1127.192
#GB     1194.586
#USA    1161.795
#or
 do.call(rbind,lapply(unique(dat1$country),function(x) {subframe<- dat1[which(dat1$country==x),]; fit<- lm(income~0+age,data=subframe); Coef1<-data.frame(age=coef(fit)); row.names(Coef1)<-x; Coef1}))
#            age
#USA    1161.795
#GB     1194.586
#France 1127.192
A.K.

----- Original Message -----
From: "CHEN, Cheng" <chencheng at gmail.com>
To: R-help at r-project.org
Cc: 
Sent: Sunday, May 19, 2013 8:31 AM
Subject: [R] How to run lm for each subset of the data frame,
 and then aggreage the result?

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 country. Then aggregate the result into a new data
frame looks like :

    countryname,  coeffname1      USA         1.22      GB
1.03      France      1.1

I tried to do :
do.call("rbind", lapply(countries, fitFunc))

but this only gives something like:

          age
[1,] 2.540879
[2,] 2.428830
[3,] 2.369560
How should I proceed?

can anyone help?


-- 
*CHEN*, Cheng

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list