list2df: Coerce a list of rows to a data frame

Description Usage Arguments Examples

View source: R/list2df.R

Description

Function to coerce a list (of rows) to a data frame.

Usage

1
list2df(x, col_names, ..., stringsAsFactors = default.stringsAsFactors())

Arguments

x

A list. Each element corresponds to a row of the upcoming data frame.

col_names

Optional. If not provided and if the first list element is a named vector, this vector is used to name the columns of the data frame.

stringsAsFactors

Logical. Should the character vector be converted to a factor?

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
my_list <- list(c(23, 21, 41), c(3, 55), 8, c(39, 31, 14))
list2df(my_list)

# Name the columns:
# - Option #1:
list2df(my_list, col_names = letters[1:3])
# - Option #2:
my_list <- list(c(a = 23, b = 21, c = 41), c(d = 3, e = 55), c(f = 8),
                c(g = 39, h = 31, i = 14))
list2df(my_list)

# A more tricky case:
my_list <- list(r1 = c(a = 23, 21), r2 = c(c = 41, d = 3, e = 55),
                r3 = c(f = 8), r4 = c(g = 39, h = 31, i = 14))
list2df(my_list)

chgigot/cgmisc documentation built on May 14, 2019, 8:17 a.m.