De Castro Pascual, Montserrat
2013-Apr-29 13:54 UTC
[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]]
David Winsemius
2013-Apr-29 15:46 UTC
[R] rbinding some elements from a list and obtain another list
On Apr 29, 2013, at 6:54 AM, De Castro Pascual, Montserrat wrote:> 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'm looking at this apparently malformed command and wondering if that is the root of all your problems. Do you know how to make a simple successful example of a list of dataframes? -- David.> 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 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.David Winsemius Alameda, CA, USA
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 at creal.cat>
To: r-help at 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 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.
MacQueen, Don
2013-Apr-29 20:39 UTC
[R] rbinding some elements from a list and obtain another list
In addition to the other responses, consider this:> i <- 3 > i:i+1[1] 4> i:(i+1)[1] 3 4 -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 4/29/13 6:54 AM, "De Castro Pascual, Montserrat" <mdecastro at creal.cat> wrote:>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]] >