Skip to content

How to intantiate a list of data.frames?

7 messages · Rui Maximo, Joshua Wiley

#
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:

  
    
#
Hi Rui,

data frames must have the same number of rows, but two different data
frames stored within a list do not need to have the same number of
rows.  Can you please post the code that is giving the error?

Josh
On Sun, May 22, 2011 at 9:41 AM, Rui Maximo <ruimaximo at hotmail.com> wrote:

  
    
2 days later
#
Hi Rui,

Please look at the documentation for ?write.csv

I do not have oilDF, but my guess is that you make the object, "ds"
fine, but then you are trying to pass a list to write.csv which works
on matrices or data frames (or attempts to coerce to such).  The
easiest answer is probably to write each element of "ds" (that is,
each data frame) to a separate file.

Cheers,

Josh
On Sun, May 22, 2011 at 12:11 PM, Rui Maximo <ruimaximo at hotmail.com> wrote: