[R] create data frame(s) from a list with different numbers of rows
jim holtman
jholtman at gmail.com
Fri Sep 28 01:44:20 CEST 2007
Instead of:
sample.df1 <-data.frame(list.sample[[1]])
sample.df2 <-data.frame(list.sample[[2]])
sample.df3 <-data.frame(list.sample[[3]])
sample.df4 <-data.frame(list.sample[[4]])
sample.df5 <-data.frame(list.sample[[5]])
sample.df6 <-data.frame(list.sample[[6]])
use 'lapply' to create a list of dataframe:
> lapply(list.sample, data.frame)
[[1]]
Var1 Freq
1 1 1 1 0.5
1 3 1 3 0.5
1 4 1 4 0.5
2 1 2 1 0.5
2 2 2 2 0.5
2 3 2 3 0.5
2 4 2 4 1.0
3 2 3 2 1.5
3 3 3 3 0.5
...........
You will have a list of 6 dataframes that you can then use.
On 9/27/07, Luke Neraas <lukasneraas.r at gmail.com> wrote:
> # Hello,
>
>
> # I have a list with 6 categories and with different numbers of rows.
> # I would like to change each of them into a unique data frame in order to
> match
> # values with other data frames and perform some calculations.
> # Or I could make each category or list element have the same number of rows
> and create one large data.frame.
> # below is a creation of a sample list
> # I apologize for the length in creating the list it is the only way i could
> figure
> # out how to convey my puzzle. If you scroll down you will find it as "
> list.sample."
>
>
> Loci<-4
> Pairwise<- (Loci*(Loci-1))/2
>
> #Creation of list elements
>
> c1<- c(1,4,3,2,4,1,3,2,4,3)
> c2<- c(2,4,3,4,2,3,4,1,3,2)
> c3<- c(1,3,2,4,4,3,4,4,2,2)
> c4<- c(2,3,2,3,1,3,2,4,4,3)
> c5<- c(1,2,1,1,2,2,3,3,2,1)
> c6<- c(3,2,4,3,1,1,2,3,3,4)
> c7<- c(1,2,1,2,3,2,3,2,1,2)
> c8<- c(1,2,2,3,2,3,3,4,1,2)
>
> List.elements<-cbind(c1,c2,c3,c4,c5,c6,c7,c8)
>
>
> #Locus 1
> L1.pairwise.columns <- matrix(3:ncol(List.elements), byrow=TRUE, ncol=2)
>
> Loc1.gamete.counts<- apply(L1.pairwise.columns , 1, function(.row){
> paste(List.elements[,1:2], List.elements[,.row])
> })
> #Locus2
> L2.pairwise.columns <- matrix(5:ncol(List.elements), byrow=TRUE, ncol=2)
>
> Loc2.gamete.counts<- apply(L2.pairwise.columns , 1, function(.row){
> paste(List.elements[,3:4], List.elements[,.row])
> })
> #Locus3
> L3.pairwise.columns <- matrix(7:ncol(List.elements), byrow=TRUE, ncol=2)
>
> Loc3.gamete.counts<- apply(L3.pairwise.columns , 1, function(.row){
> paste(List.elements[,5:6], List.elements[,.row])
> })
> ##Creation of the List
>
> Gamete.pairs<-cbind(Loc1.gamete.counts,Loc2.gamete.counts,Loc3.gamete.counts
> )
> Gamete.pairs
>
> Gamete.list<-list(1,2,3,4,5,6)
>
> for (i in 1:Pairwise){
> Gamete.list[[i]]<-table(Gamete.pairs[,i])
> }
> Gamete.list
>
> #### Gamete Frequency
>
> list.sample<-list(1,2,3,4,5,6)
>
> for (j in 1:Pairwise){
> list.sample[[j]]<- Gamete.list[[j]]/(2*Genetic.Sample.Size)
> }
>
> ######## Here is the Sample List
>
> list.sample
>
>
> # I would like to have a flexible way to turn all six element in my list
> # into separate data frames so i can do some calculations
> # the only way i can figure out how to do this is "one by one" in very
> clunky fashion.
> # here is an example of my code
>
> sample.df1 <-data.frame(list.sample[[1]])
> sample.df2 <-data.frame(list.sample[[2]])
> sample.df3 <-data.frame(list.sample[[3]])
> sample.df4 <-data.frame(list.sample[[4]])
> sample.df5 <-data.frame(list.sample[[5]])
> sample.df6 <-data.frame(list.sample[[6]])
>
>
> sample.df1
> sample.df2
> sample.df3
> sample.df4
> sample.df5
> sample.df6
>
> # In the future i will have up to 1,200 of these small dataframes to create.
> # is there a way to loop through the list and create many small data
> frames??
> # or perhaps make each of the list elements the same length and create one
> large data frame??
>
> # any help or ideas would be greatly appreciated
>
> # thanks in advance
>
> Luke Neraas
>
> lukasneraas.r at gmail.com
>
> University of Alaska Fairbanks
> School of Fisheries and Ocean Sciences
> 11120 Glacier Highway
> UAF Fisheries Division
> Juneau, AK 99801
>
> [[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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list