Dan Abner
2012-Jan-08 14:43 UTC
[R] Convert components of a list to separate columns in a data frame or matrix XXXX
Hello everyone, What is the most efficient & simpliest way to convert all components of a list to separate columns in a matrix? Is there an easy way to programmatically "pad" the length of the resulting shorter character vectors so that they can be easily combined into a data frame? I have the following code that stores the 2 compoents (of differing lengths) in the same character vector: more<-c('R is a free software environment for statistical computing', 'It compiles and runs on a wide variety of UNIX platforms') result<-strsplit(more,' ') result mode(result) class(result) sapply(result,length) result2<-unlist(result) result2 mode(result2) class(result2) Thank you, Dan [[alternative HTML version deleted]]
jim holtman
2012-Jan-08 19:28 UTC
[R] Convert components of a list to separate columns in a data frame or matrix XXXX
Is this what you are after:> more<-c('R is a free software environment for statistical computing',+ 'It compiles and runs on a wide variety of UNIX platforms')> result<-strsplit(more,' ') > result[[1]] [1] "R" "is" "a" "free" "software" "environment" "for" [8] "statistical" "computing" [[2]] [1] "It" "compiles" "and" "runs" "on" "a" "wide" "variety" [9] "of" "UNIX" "platforms"> # determine the longest length to which to pad > maxLen <- max(sapply(result, length)) > # now pad the vectors in the list > newResult <- lapply(result, function(x) c(x, rep(NA, maxLen - length(x)))) > # now create your matrix > newDF <- do.call(data.frame, newResult) > # add short names > names(newDF) <- paste("V", seq_len(length(result)), sep = '') > str(newDF)'data.frame': 11 obs. of 2 variables: $ V1: Factor w/ 9 levels "a","computing",..: 7 6 1 5 8 3 4 9 2 NA ... $ V2: Factor w/ 11 levels "a","and","compiles",..: 4 3 2 8 6 1 11 10 5 9 ...> newDFV1 V2 1 R It 2 is compiles 3 a and 4 free runs 5 software on 6 environment a 7 for wide 8 statistical variety 9 computing of 10 <NA> UNIX 11 <NA> platforms>On Sun, Jan 8, 2012 at 9:43 AM, Dan Abner <dan.abner99 at gmail.com> wrote:> Hello everyone, > > What is the most efficient & simpliest way to convert all components of a > list to separate columns in a matrix? > > Is there an easy way to programmatically "pad" the length of the resulting > shorter character vectors so that they can be easily combined into a data > frame? > > I have the following code that stores the 2 compoents (of differing > lengths) in the same character vector: > > more<-c('R is a free software environment for statistical computing', > ? ? ? ? 'It compiles and runs on a wide variety of UNIX platforms') > result<-strsplit(more,' ') > result > mode(result) > class(result) > sapply(result,length) > result2<-unlist(result) > result2 > mode(result2) > class(result2) > > Thank you, > > Dan > > ? ? ? ?[[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 Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Rui Barradas
2012-Jan-08 19:31 UTC
[R] Convert components of a list to separate columns in a data frame or matrix XXXX
Hello, I believe this solves your problem fun <- function(x){ f <- function(x, n){ if(length(x) < n) x <- c(x, rep(NA, n-length(x))) return(x) } lapply(x, f, max(unlist(lapply(x, length)))) } fun(result) # the 'result' list above list2 <- list(x=1:3, y=1:5, z="abc") fun(list2) The function returns a list, then it can be made a matrix, a data.frame or whatever. Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Convert-components-of-a-list-to-separate-columns-in-a-data-frame-or-matrix-XXXX-tp4275790p4276510.html Sent from the R help mailing list archive at Nabble.com.