listVectors2data.frame: Convert a list of vectors to a data frame.

Description Usage Arguments Details Value Author(s) References Examples

View source: R/listVectors2data.frame.R

Description

This function will convert a list of vectors to a data frame. This function will handle three different types of lists of vectors. First, if all the elements in the list are named vectors, the resulting data frame will have have a number of columns equal to the number of unique names across all vectors. In cases where some vectors do not have names in other vectors, those values will be filled with NA.

Usage

1

Arguments

x

a list to convert to a data frame.

row.names

a vector equal to length(x) corresponding to the row names. If NULL, the row names will be set to names(x).

optional

not used.

...

other parameters passed to data.frame.

Details

The second case is when all the vectors are of the same length. In this case, the resulting data frame is equivalent to applying rbind across all elements.

The third case handled is when there are varying vector lengths and not all the vectors are named. This condition should be avoided. However, the function will attempt to convert this list to a data frame. The resulting data frame will have a number of columns equal to the length of the longest vector. For vectors with length less than this will fill the row with NAs. Note that this function will print a warning if this condition occurs.

Value

a data frame.

Author(s)

Jason Bryer <<[email protected]>>

References

http://stackoverflow.com/questions/4227223/r-list-to-data-frame

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    test1 <- list( c(a='a',b='b',c='c'), c(a='d',b='e',c='f'))
    HexSimR::listVectors2data.frame(test1)
    
    test2 <- list( c('a','b','c'), c(a='d',b='e',c='f'))
    HexSimR::listVectors2data.frame(test2)
    
    test3 <- list('Row1'=c(a='a',b='b',c='c'), 'Row2'=c(var1='d',var2='e',var3='f'))
    HexSimR::listVectors2data.frame(test3)
    
    test4 <- list('Row1'=letters[1:5], 'Row2'=letters[1:7], 'Row3'=letters[8:14])
    HexSimR::listVectors2data.frame(test4)
    
    test5 <- list(letters[1:10], letters[11:20])
    HexSimR::listVectors2data.frame(test5)
    

carlopacioni/HexSimR documentation built on Nov. 11, 2018, 8:33 p.m.