I have a set of character vectors of uneven length that I have stored in a list. I can easily enough get any column of them using lapply but what I want is to be able to create a matrix of them. Other than some kind of brute force looping approach I have drawn a blank. Would somebody please suggest something? Thanks Example. mylist <- list(aa=c("cat","peach" ), bb=c("dog", "apple", "iron"), cc = c("rabbit", "orange", "zinc", "silk")) myfun <- function(dff) dff[1] vect1 <- unlist(lapply(mylist, myfun)) Looking for the perfect gift? Give the gift of Flickr!
What would you want your output matrix to look like given mylist? John Kane wrote:> I have a set of character vectors of uneven length > that I have stored in a list. I can easily enough get > any column of them using lapply but what I want is to > be able to create a matrix of them. Other than some > kind of brute force looping approach I have drawn a > blank. > > Would somebody please suggest something? Thanks > > Example. > > mylist <- list(aa=c("cat","peach" ), bb=c("dog", > "apple", "iron"), > cc = c("rabbit", "orange", "zinc", "silk")) > myfun <- function(dff) dff[1] > vect1 <- unlist(lapply(mylist, myfun)) > > > Looking for the perfect gift? Give the gift of Flickr! > > ______________________________________________ > 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.
If I understand correctly, try this: as.data.frame(lapply(mylist, `[`, 1:max(unlist(lapply(mylist, length))))) On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:> I have a set of character vectors of uneven length > that I have stored in a list. I can easily enough get > any column of them using lapply but what I want is to > be able to create a matrix of them. Other than some > kind of brute force looping approach I have drawn a > blank. > > Would somebody please suggest something? Thanks > > Example. > > mylist <- list(aa=c("cat","peach" ), bb=c("dog", > "apple", "iron"), > cc = c("rabbit", "orange", "zinc", "silk")) > myfun <- function(dff) dff[1] > vect1 <- unlist(lapply(mylist, myfun)) > > > Looking for the perfect gift? Give the gift of Flickr! > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Ideally something like this: =====================================================t(cbind( c("cat" , "peach" , NA, NA), bbb <- c("dog" , "apple" ,"iron", NA), ccb <- c("rabbit" ,"orange" ,"zinc" , "silk" ))) ===================================================== Thanks --- Erik Iverson <iverson at biostat.wisc.edu> wrote:> What would you want your output matrix to look like > given mylist? > > John Kane wrote: > > I have a set of character vectors of uneven length > > that I have stored in a list. I can easily enough > get > > any column of them using lapply but what I want > is to > > be able to create a matrix of them. Other than > some > > kind of brute force looping approach I have drawn > a > > blank. > > > > Would somebody please suggest something? Thanks > > > > Example. > > > > mylist <- list(aa=c("cat","peach" ), bb=c("dog", > > "apple", "iron"), > > cc = c("rabbit", "orange", "zinc", > "silk")) > > myfun <- function(dff) dff[1] > > vect1 <- unlist(lapply(mylist, myfun)) > > > > > > Looking for the perfect gift? Give the gift > of Flickr! > > > > ______________________________________________ > > 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. >[[elided trailing spam]]