list2df: Convert a list to a data frame

Description Usage Arguments Details Value Author(s) Examples

View source: R/list2df.R

Description

Convert a list of vectors (or data frames) with same numbered lengths (or number of columns) into a data frame.

Usage

1
2
list2df(vList, col.names = NULL, row.names = NULL, convert.numeric = TRUE,
  strings.as.factors = FALSE)

Arguments

vList

List of vectors, data frames, or lists. See Details.

col.names

Optional character vector of length n with column names that will be given to the output data frame. If col.names = NULL, column names are extracted if possible from the column names (or names) of the data frames (or vectors).

row.names

Optional character vector with length equivalent to the length of vList containing the row names of the output data frame. If row.names = NULL, row names from the data frames (or names of the vList elements) if possible.

convert.numeric

If vList is list of vectors, = TRUE attempts to convert each column to numeric if possible using as.numericSilent

strings.as.factors

If vList is a list of vectors or lists, = FALSE converts factors into characters using factor2character.

Details

If the elements of vList are vectors, each vector must have the same length, n, and the resulting data frame will have n columns. If the elements of vList are data frames, each data frame must have the same structure (though they may have differing numbers of rows). If the elements of vList are lists, each list is first converted to a data frame via as.data.frame and the resulting data frames must have the same structure (though they may have differing numbers of rows).

It is permissible for vList to contain NULL elements. list2df performs numerous consistency checks to ensure that contents of vList which are combined into the resulting data frame are conformable, labeled consistently, of the equivalent class when necessary, etc.

Value

If vList is list of data frames, a data frame resulting from efficiently row binding the data frames in vList is returned. If vList is a list of vectors, a data frame is returned where the first column contains the first elements of the list vectors, the second column contains the second elements of the list vectors, etc.

Author(s)

Landon Sego

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# For a list of vectors
x <- c("r1c1 1", "r2c1 2", "r3c1 3", "r4c4 4")
y <- strsplit(x, "\ ")
y
list2df(y)
list2df(y, col.names = LETTERS[1:2])

# Here's another list of vectors
z <- list(NULL, a = c(first = 10, second = 12), NULL, b = c(first = 15, second = 17))
z
list2df(z)

# For a list of data frames
z <- list(d1 = data.frame(a = 1:4, b = letters[1:4]),
          d2 = data.frame(a = 5:6, b = letters[5:6]))
z
list2df(z)

# A list of lists
z <- list(list(a = 10, b = TRUE, c = "hi"), list(a = 12, b = FALSE, c = c("there", "bye")))
z
list2df(z)

Smisc documentation built on May 2, 2019, 2:46 a.m.