[R] creating a "list of 3 dataframes" from a "list of 2 dataframes" and a dataframe?

Petr Klasterecky klaster at karlin.mff.cuni.cz
Mon May 14 23:25:31 CEST 2007


Rajarshi Guha napsal(a):
> On May 14, 2007, at 2:48 PM, new ruser wrote:
> 
>> #I wish to create a "list of three dataframes" ("results2")  from a  
>> "list of two dataframes" (temp) and a dataframe ("c")?
>>
>> #Please advise.
>>
>> a <- data.frame(x=c(1,2,3),y=c(5,7,9))
>> b <- data.frame(x=c(2,4,7,9),y=c(2,3,5,4))
>> c <- data.frame(x=c(22,34,7,9),y=c(52,63,5,4))
>> results1 <- list(a,b,c) #what I want
>>
>> #but this is how I need to get there
>> temp <- list(a,b)
>> #results2 <- fun?????(temp2,c) # I need help here
> 
> temp[[ length(temp) + 1 ]] <- c
> results2 <- temp

A MUCH cleaner solution comes directly from the help for c():

## append to a list:
      ll <- list(A = 1, c="C")
      ## do *not* use
      c(ll, d = 1:3) # which is == c(ll, as.list(c(d=1:3))
      ## but rather
      c(ll, d = list(1:3))# c() combining two lists

So in your case
results2 <- c(temp, list(c))

Petr




> 
> -------------------------------------------------------------------
> Rajarshi Guha  <rguha at indiana.edu>
> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04  06F7 1BB9 E634 9B87 56EE
> -------------------------------------------------------------------
> Does Ramanujan know Polish?
>                 --  E.B. Ross
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list