Transforming list to data frame with corresponding columns

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Transforming list to data frame with corresponding columns

Josh R.
Hi all,
   Consider the following.


a<-list(c("MHsub","MHsub","SFD","Duplex"))
b<-list(c("MHsub","MHsub","SFD"))
c<-list(c("MHpark","SFD","SFD"))
d<-list(c("MultiFam","MultiFam","MultiFam","Duplex"))

all<-list(a,b,c,d)
names(all)<-c("5","6","7","8")
all2<-list()
for(i in 1:length(all)){
all2[[i]]<-table(unlist(all[i]))
}
names(all2)<-names(all)

What i want to do is to put each of the elements of the list into a data frame where the element represents a row and each vector is accounted for, for all types within the entire list.  So where there isnt a particular vector a 0 is noted so that the final product looks like this:


        SFD Mhsub Duplex MultiFam
5 1   1            1               0
6 1   1            0            0
7 1   1            0            0
8 0   0            1            1

Thanks for the insights.  
 
Reply | Threaded
Open this post in threaded view
|

Re: Transforming list to data frame with corresponding columns

Henrique Dallazuanna
Try this:

`colnames<-`(do.call(rbind, lapply(all2, '[', nm <-
unique(unlist(sapply(all2, names))))), nm)

On Mon, Jun 7, 2010 at 4:20 PM, LCOG1 <[hidden email]> wrote:

>
> Hi all,
>   Consider the following.
>
>
> a<-list(c("MHsub","MHsub","SFD","Duplex"))
> b<-list(c("MHsub","MHsub","SFD"))
> c<-list(c("MHpark","SFD","SFD"))
> d<-list(c("MultiFam","MultiFam","MultiFam","Duplex"))
>
> all<-list(a,b,c,d)
> names(all)<-c("5","6","7","8")
> all2<-list()
> for(i in 1:length(all)){
> all2[[i]]<-table(unlist(all[i]))
> }
> names(all2)<-names(all)
>
> What i want to do is to put each of the elements of the list into a data
> frame where the element represents a row and each vector is accounted for,
> for all types within the entire list.  So where there isnt a particular
> vector a 0 is noted so that the final product looks like this:
>
>
>        SFD     Mhsub   Duplex  MultiFam
> 5       1          1                1               0
> 6       1          1                0               0
> 7       1          1                0               0
> 8       0          0                1               1
>
> Thanks for the insights.
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Transforming-list-to-data-frame-with-corresponding-columns-tp2246423p2246423.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

RE: Transforming list to data frame with corresponding columns

Josh R.
Henrique,
  The last message gave me exactly what i needed.  Im still not sure whats going on, i will need to reference the do.call function some more.  Thanks for your help. 
 

Josh Roll
Lane Council of Governments
Transportation Planner
Office: 541-682-2454
Cell: 406-671-5069
[hidden email]
www.lcog.org



From: Henrique Dallazuanna [via R] [mailto:[hidden email]]
Sent: Monday, June 07, 2010 2:31 PM
To: ROLL Josh F
Subject: Re: Transforming list to data frame with corresponding columns

Try this:

`colnames<-`(do.call(rbind, lapply(all2, '[', nm <-
unique(unlist(sapply(all2, names))))), nm)

On Mon, Jun 7, 2010 at 4:20 PM, LCOG1 <[hidden email]> wrote:

>
> Hi all,
>   Consider the following.
>
>
> a<-list(c("MHsub","MHsub","SFD","Duplex"))
> b<-list(c("MHsub","MHsub","SFD"))
> c<-list(c("MHpark","SFD","SFD"))
> d<-list(c("MultiFam","MultiFam","MultiFam","Duplex"))
>
> all<-list(a,b,c,d)
> names(all)<-c("5","6","7","8")
> all2<-list()
> for(i in 1:length(all)){
> all2[[i]]<-table(unlist(all[i]))
> }
> names(all2)<-names(all)
>
> What i want to do is to put each of the elements of the list into a data
> frame where the element represents a row and each vector is accounted for,
> for all types within the entire list.  So where there isnt a particular
> vector a 0 is noted so that the final product looks like this:
>
>
>        SFD     Mhsub   Duplex  MultiFam
> 5       1          1                1               0
> 6       1          1                0               0
> 7       1          1                0               0
> 8       0          0                1               1
>
> Thanks for the insights.
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Transforming-list-to-data-frame-with-corresponding-columns-tp2246423p2246423.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.



View message @ http://r.789695.n4.nabble.com/Transforming-list-to-data-frame-with-corresponding-columns-tp2246423p2246612.html
To unsubscribe from Transforming list to data frame with corresponding columns, click here.