[R] How to intantiate a list of data.frames?

Joshua Wiley jwiley.psych at gmail.com
Sat May 21 20:46:05 CEST 2011


Hi Rui,

Here is one option:

ds <- vector("list", 6)
for(i in 1:6) ds[[i]] <- list(df = mtcars[, c(i, i + 2)], df2 =
mtcars[, c(i, i + 2)] + 10)

another could be:

altds <- lapply(1:6, function(x) {
  list(df = mtcars[, c(x, x + 2)], df2 = mtcars[, c(x, x + 2)] + 10)
})

all.equal(ds, altds)

For some documentation, see

?vector
?lapply

Cheers,

Josh

On Sat, May 21, 2011 at 10:47 AM, Rui Maximo <ruimaximo at hotmail.com> wrote:
>
> Hello,
>
>  I am newbie to R and I want to do this:
>
> for(i in 1:6)
> {
>        ds[i] <- list(df=data.frame(oilDF[,1],oilDF[,i+2]), df2=data.frame(oilDF2[,1],oilDF2[,i+2]))
> }
>
> #oilDF and oilDF2 are 2 data frames with several columns. They have different number of rows
>
> #I want to have for example ds[1]$df, ds[1]$df2 with the respective data.frames.
> #How can I instantiate a list of data.frames pairs with different number of rows?
>
> Thank you,
> Rui
>
>        [[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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list