[R] apply a function separately on each element of a list

jeff6868 geoffrey_klein at etu.u-bourgogne.fr
Fri Aug 24 14:47:12 CEST 2012


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.



More information about the R-help mailing list