[R] rbinding some elements from a list and obtain another list

De Castro Pascual, Montserrat mdecastro at creal.cat
Tue Apr 30 09:30:20 CEST 2013


BINGO!!!
Many thanks arun!
You are a genious!!!

montserrat

-----Mensaje original-----
De: arun [mailto:smartpink111 en yahoo.com]
Enviado el: lunes, 29 de abril de 2013 18:33
Para: De Castro Pascual, Montserrat
CC: R help
Asunto: Re: [R] rbinding some elements from a list and obtain another list

Hi,
Try this:
set.seed(24)
 lst1<-lapply(1:4,function(x) 
as.data.frame(matrix(sample(1:20,20,replace=TRUE),ncol=5)))
 names(lst1)<- LETTERS[1:4]

res<-lapply(list(c("A","B"),c("C","D")), function(x) do.call(rbind,lst1[x]))
 res
#[[1]]
 #   V1 V2 V3 V4 V5
#A.1  6 14 17 14  4
#A.2  5 19  6 14  1
#A.3 15  6 13  7 11
#A.4 11 16  8 19  3
#B.1  2  5 13  8 15
#B.2 12 14  1  3 13
#B.3 15  2  7 19 14
#B.4  3 12  5  5 20
#
#[[2]]
 #   V1 V2 V3 V4 V5
#C.1 10  1  6 10 10
#C.2  8  2  7 15  6
#C.3  6  8 10 11  4
#C.4  5  8 18 20  3
#D.1 10 15 15  1 12
#D.2  5  7 10 20 17
#D.3  6 19  3 13  1
#D.4  3 20  5  7 15
A.K.





----- Original Message -----
From: "De Castro Pascual, Montserrat" <mdecastro en creal.cat>
To: r-help en r-project.org
Cc:
Sent: Monday, April 29, 2013 9:54 AM
Subject: [R] rbinding some elements from a list and obtain another list

Hi everybody,



I have a list, where every element of this list is a data frame.



An example:



Mylist<-list(A=data.frame, B=data.frame, C=data.frame, D=data.frame)



I want to rbind some elements of this list.

As an example:



Output<-list(AB=data.frame, CD=data.frame)



Where

AB=rbind(A,B)

CD=rbind(C,D)





I’ve tried:



f<-function(x){

  for (i in seq(1,length(names(x)),2)){

    aa<-do.call(rbind,x[i:i+1])

    aa

  }}

bb<-f(mylist)



or



f<-function(x){

  for (i in seq(1,length(names(x)),2)){

    aa[i]<-do.call(rbind,x[i:i+1])

    list(aa[i])

    }}

bb<-f (mylist)



but it doesn’t works!!!!



f<-function(x){

+   for (i in seq(1,length(names(x)),2)){

+     aa<-do.call(rbind,x[i:i+1])

+     aa

+   }}

> bb<-f(mylist)

> bb

NULL

> f<-function(x){

+   for (i in seq(1,length(names(x)),2)){

+     aa<-do.call(rbind,x[i:i+1])

+     aa

+   }}

> bb<-f(mylist)

>

> f<-function(x){

+   for (i in seq(1,length(names(x)),2)){

+     aa[i]<-do.call(rbind,x[i:i+1])

+     list(aa[i])

+   }}

> bb<-f(mylist)

Mensajes de aviso perdidos

1: In aa[i] <- do.call(rbind, x[i:i + 1]) :

  número de items para para sustituir no es un múltiplo de la longitud del 
reemplazo

2: In aa[i] <- do.call(rbind, x[i:i + 1]) :

  número de items para para sustituir no es un múltiplo de la longitud del 
reemplazo

3: In aa[i] <- do.call(rbind, x[i:i + 1]) :

  número de items para para sustituir no es un múltiplo de la longitud del 
reemplazo

4: In aa[i] <- do.call(rbind, x[i:i + 1]) :

  número de items para para sustituir no es un múltiplo de la longitud del 
reemplazo

5: In aa[i] <- do.call(rbind, x[i:i + 1]) :

  número de items para para sustituir no es un múltiplo de la longitud del 
reemplazo

6: In aa[i] <- do.call(rbind, x[i:i + 1]) :

  número de items para para sustituir no es un múltiplo de la longitud del 
reemplazo





Thanks!



Montserrat




    [[alternative HTML version deleted]]


______________________________________________
R-help en 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.



More information about the R-help mailing list