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

Joshua Wiley jwiley.psych at gmail.com
Sun May 22 20:33:38 CEST 2011


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:
> Hi Josh,
>
> Sorry, your examples have equal number of rows in both df and df2.
> In my situation they haven't.
> Strangely, your solution have worked only when I am copy post the code into
> the command line.
> If I use the code inside of a function I get an error at:
> return(ds)
> ERROR: arguments imply differing number of rows
>
> Thanks,
> Rui
>
>> Date: Sat, 21 May 2011 11:46:05 -0700
>> Subject: Re: [R] How to intantiate a list of data.frames?
>> From: jwiley.psych at gmail.com
>> To: ruimaximo at hotmail.com
>> CC: r-help at r-project.org
>>
>> 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/
>



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



More information about the R-help mailing list