listToDT: Convert List to data.table

View source: R/listToDT.R

listToDTR Documentation

Convert List to data.table

Description

Transform a list of vectors into a data.table. Vectors can be of differing lengths (shorter vectors padded with NA). Each list element becomes a column (or row) in the new table. Note that classes will likely not be maintained when joining by row.

Usage

listToDT(input_lsv, bind_v = "col", name_v = NULL)

Arguments

input_lsv

list of vectors. Can be of varying lengths and classes.

bind_v

One of either 'col' (default) or 'row'. Indicating if list elements should be joined by column or row.

name_v

character string for the name of the extra column added when joining by row. Only used when bind_v == "row". If NULL (default), column name will default to V1.

Value

data.table object whose format depends on bind_v value. For bind_v == 'col': each column corresponds to an element of the input list. Number of rows in output data.table is equal to the length of the longest vector in the input list. Colnames are the same as the list element names.

For bind_v == 'row': each row corresponds to an element of the input list. Number of columns in output data.table is one more than the length of the longest vector in the input list, with the extra column containing the list element names.

Examples

list1_ls <- list("A" = 1:5, "B" = LETTERS[1:10], "C" = letters[11:16])
list2_ls <- list("2" = 10:1, "5" = LETTERS[1:10])
list1_dt <- listToDT(list1_ls)
list2_dt <- listToDT(list2_ls)
rList1_dt <- listToDT(list1_ls, bind_v = "row")
rList1_dt2 <- listToDT(list1_ls, bind_v = "row", name_v = "Test")

weshorton/wrh.rUtils documentation built on Oct. 28, 2024, 7:24 a.m.