colin
colin

Reputation: 2666

run a function on several data sets using a for loop in R

Say I have several datasets, d1,d2,d3, stored as a list:

a<-1:10
d1<-data.frame(a)
d2<-data.frame(a)
d3<-data.frame(a)
d1$b<-a*1
d2$b<-a*2
d3$b<-a*3

list<-c(d1,d2,d3)

I then have a function, that fits a regression.

fxn<- function(param1, param2, dataset){
    mod<-lm(dataset$param1~dataset$param2)
    return(coef(mod)[2])
}

I would like to feed this function the parameters to use for the regression (a and b) and then the list of data sets stored in list. Please note that this function doesn't work as I would like it to at the moment. I want it to calculate the dataset specific fits and return the slopes.

Upvotes: 1

Views: 179

Answers (1)

LyzandeR
LyzandeR

Reputation: 37879

I assume you would like the function to be like this then:

fxn<- function(param1, param2, dataset){
  #run model according to param1 and param2
  mod<-lm(dataset[,param1]~dataset[,param2])
  #calculate slope
  slope <- coef(mod)[2]
  #name it correctly
  names(slope) <- param2
  return(slope)
}

Then you could use an lapply to use the function for each data.frame:

lapply(list, function(x) fxn('a','b',x))

Output:

[[1]]
b 
1 

[[2]]
  b 
0.5 

[[3]]
        b 
0.3333333 

Data:

a<-1:10
d1<-data.frame(a)
d2<-data.frame(a)
d3<-data.frame(a)
d1$b<-a*1
d2$b<-a*2
d3$b<-a*3

#made a small change
#stored the data.frames in a list 
#as I assume you probably wanted
list<-list(d1,d2,d3)

Edit:

So, in order to create the format you mention in your comment you could do:

#first have the data.frames in a named list
list<-list(d1=d1,d2=d2,d3=d3)

and then do:

temp <- data.frame(t(data.frame(lapply(list, function(x) fxn('a','b',x)))))
#and if you want rownames as a column
temp$dfs <- row.names(temp)

> temp
           b dfs
d1 1.0000000  d1
d2 0.5000000  d2
d3 0.3333333  d3

Upvotes: 2

Related Questions