Skip to content

apply a function separately on each element of a list

4 messages · Daniel Malter, Rui Barradas, jeff6868

#
Hi everybody,

I have a question about applying a specific function (with the calculations
I want to do), on a list of elements.

Each elements are like a data.frame (with nrows and ncolumns), and have the
same structure.
At frist, I had a big data.frame that I splitted in all my elements of my
list. They have been splitted by day.
For example, the name of the first element of my list is "2011-01-01", and
is a data.frame corresponding to all my data from this specific date. Then
my second element is "2011-01-02", etc....

My question is: how can I apply a function on each element separately (a bit
like a loop)?

For example, if my data from the first element "2011-01-01" is:
element1 <- data.frame(x=rnorm(1:10),data=c(1:10))

I would like to do a regression between "x" and "data (so lm(data ~x) ), to
get the predicted values of the regression, and then to keep the results in
a new object.

And then, do the same with the second element (regression between "x" and
"data" of the second element), keep the results of the predicted values and
keep the results.

... and so one with 200 elements.

Is there any way to do this?

Thanks a lot!



--
View this message in context: http://r.789695.n4.nabble.com/apply-a-function-separately-on-each-element-of-a-list-tp4641186.html
Sent from the R help mailing list archive at Nabble.com.
#
The easiest way may be to use lmList in the nlme library:

#simulate data
d<-rep(1:10,each=10)
x<-rnorm(100)
e<-rnorm(100)
y<-2*x+e

require(nlme) #or install and load package

lmList(y~x|d)

#predicted values are obtained with:

predict(lmList(y~x|d)

HTH,
Daniel





jeff6868 wrote
--
View this message in context: http://r.789695.n4.nabble.com/apply-a-function-separately-on-each-element-of-a-list-tp4641186p4641204.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

Your example doesn't run, lmList needs a 'data' argument.

set.seed(8109)
d <- rep(1:10, each=10)
x <- rnorm(100)
e <- rnorm(100)
y <- 2*x + e

dat <- data.frame(x=x, y=y, d=d, e=e)

model <- lmList(y ~ x | d, data = dat)
predict(model)

To the op: if you already have the larger data.farme divided into a list 
of smaller df's, such as the 'lst' below, you can use lapply.


df.lst <- split(dat, dat$d)


model.lst <- lapply(df.lst, function(.data) lm(y ~ x, data = .data))
lapply(model.lst, coef)  # just an example
lapply(model.lst, predict)  # what you want

Hope this helps,

Rui Barradas

Em 24-08-2012 16:05, Daniel Malter escreveu:
3 days later
#
Yes, this is it (as would say michael)! Thank you guys!

Last question about another function on this list: imagine this list is my
data after your function for the regression model:

mydf <- data.frame(x=c(1:5), y=c(21:25),z=rnorm(1:5))
mylist <- rep(list(mydf),5) 

Don't care about this fake data, it's just for the example. I've my results
in column "z" (from regression) for each DF of the list, and 2 other columns
"x" and "y" representing some spatial coordinates.

I have another independent DF containing a list of "x" and "y" too,
representing some specific regions (imagine 10 regions):
region <- data.frame(x=c(1:10),y=c(21:30),region=c(1:10))
 
The final aim is to have for each 10 regions, a value "z" (of my regresion)
from the nearest point of each of the DF of my list.
That means for one region: 10 results "z" from DF1 of my list, then 10 other
results "z" from DF2, ...

I already have a small function to look for the nearest value:

min.dist <- function(p, coord){
     which.min( colSums((t(coord) - p)^2) )
}

Then, I'm trying to make a loop to havewhat I want, but I have difficulties
with the list. I would need to put 2 variables in the loop, but it doesn't
works.

This works approximately if I just take 1 DF of my list:

for (j in 1:nrow(region))
	{
	 	
imin[j] <- min.dist(c(plante[j,1],plante[j,2]),mylist[[j]][,1:2])
final[j] <- mylist[[j]][imin[j], "z"]
final <- as.data.frame(final)
}

But if I select my whole list (in order to have one column of results for
each DF of the list in the object "final"), I have errors.

I think the first problem is that the length of "regions" is different of
the length of my list, and the second maybe is about adding a second
variable for the length of my list.

Is there any solution?




--
View this message in context: http://r.789695.n4.nabble.com/apply-a-function-separately-on-each-element-of-a-list-tp4641186p4641530.html
Sent from the R help mailing list archive at Nabble.com.