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

From: Joshua Wiley <jwiley.psych_at_gmail.com>
Date: Sun, 22 May 2011 11:33:38 -0700

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/

______________________________________________
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.
Received on Sun 22 May 2011 - 18:35:44 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 22 May 2011 - 21:00:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive