File: listToDfOneRow.R

package info (click to toggle)
r-cran-paramhelpers 1.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 992 kB
  • sloc: ansic: 102; sh: 13; makefile: 2
file content (16 lines) | stat: -rw-r--r-- 701 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#' @title Convert a list to a data.frame with one row
#'
#' @description
#' Convert a list of vectors or scalars to a `data.frame` with only one row. Names of the columns correspond to
#' the names of elements in the list. If a vector is one list element it is spread over multiple
#' columns and named sequentially, e.g. `a = c(5,7)` becomes `data.frame(a1 = 5, a2 = 7)`.
#'
#' @param l (`list`)\cr
#'  of atomic values of vectors.
#' @return (`data.frame`) with only one row, containing the list elements.
listToDfOneRow = function(l) {
  assertList(l, min.len = 1)
  lapply(l, assert_atomic_vector)
  l = lapply(seq_along(l), function(x) t(unlist(l[x])))
  data.frame(l, stringsAsFactors = TRUE)
}