[R] Convert components of a list to separate columns in a data frame or matrix XXXX

David Winsemius dwinsemius at comcast.net
Sun Jan 8 22:45:28 CET 2012


On Jan 8, 2012, at 2:28 PM, jim holtman wrote:

> Is this what you are after:

The code below is essentially what I would have imagined to be a  
method of programming "cbind.data.frame.fill". It's worth noting that  
either reshape2 or plyr (I don't remember which) offer an rbind  
version: rbind.data.frame.fill

-- 

David
>
>> 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 ...
>> newDF
>            V1        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.
>
> ______________________________________________
> 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, MD
West Hartford, CT



More information about the R-help mailing list