list2df: List/Matrix/Vector to Dataframe/List/Matrix

View source: R/list2df.R

list2dfR Documentation

List/Matrix/Vector to Dataframe/List/Matrix

Description

list2df - Convert a named list of vectors to a dataframe.

matrix2df - Convert a matrix to a dataframe and convert the rownames to the first column.

vect2df - Convert a named vector to a dataframe.

list_df2df - Convert a list of equal numbered/named columns to a dataframe using the list names as the level two variable.

list_vect2df - Convert a list of named vectors to a hierarchical dataframe.

counts2list - Convert a count matrix to a named list of elements.

vect2list - Convert a vector to a named list.

df2matrix - Convert a dataframe to a matrix and simultaneously move a column (default is the first column) to the rownames of a matrix.

matrix2long - Convert a matrix to a long format dataframe where column names become column 1, row names, column 2 and the values become column 3.

Usage

list2df(list.object, col1 = "X1", col2 = "X2")

matrix2df(matrix.object, col1 = "var1")

vect2df(vector.object, col1 = "X1", col2 = "X2", order = TRUE, rev = FALSE)

list_df2df(list.df.object, col1 = "X1")

list_vect2df(
  list.vector.object,
  col1 = "X1",
  col2 = "X2",
  col3 = "X3",
  order = TRUE,
  ...
)

counts2list(mat, nm = rownames(mat))

vect2list(vector.object, use.names = TRUE, numbered.names = FALSE)

df2matrix(data.frame.object, i = 1)

matrix2long(matrix.object, col1 = "cols", col2 = "rows", col3 = "vals")

Arguments

list.object

A named list of vectors..

col1

Name for column 1 (the vector elements if converting a list or the rownames if converting a matrix).

col2

Name for column 2 (the names of the vectors).

matrix.object

A matrix or simple_triplet_matrix object.

vector.object

A vector object.

order

logical. If TRUE the dataframe will be ordered.

rev

logical. If TRUE and order = TRUE the dataframe will be ordered in descending order.

list.df.object

A list of dataframes with equal number/named of columns.

list.vector.object

A list of dataframes with equal number/named of columns.

col3

The name of the third column (list_vect2df).

...

Further arguments passed to vect2df.

mat

A matrix of counts.

nm

A character vector of names to assign to the list.

use.names

logical. If TRUE and the vector is named, these names will be transferred to the list names.

numbered.names

logical. If TRUE padded numbers will be used as list names. If FALSE the vector elements themselves will become the list names.

data.frame.object

A data.frame object.

i

The column number or name to become the rownames of the matrix.

Value

list2df - Returns a dataframe with two columns.

matrix2df - Returns a dataframe.

vect2df - Returns a dataframe.

list_df2df - Returns a dataframe.

list_vect2df - Returns a dataframe.

counts2list - Returns a list of elements.

vect2list - Returns a list of named elements.

df2matrix - Returns a matrix.

matrix2long - Returns a long format dataframe.

See Also

mtabulate

Examples

lst1 <- list(x=c("foo", "bar"), y=1:5)
list2df(lst1)

lst2 <- list(a=c("hello", "everybody"), b = mtcars[1:6, 1])
list2df(lst2, "col 1", "col 2")

matrix2df(mtcars)
matrix2df(cor(mtcars))
matrix2df(matrix(1:9, ncol=3))

vect2df(1:10)
vect2df(c(table(mtcars[, "gear"])))

list_df2df(list(mtcars, mtcars))

L1 <- list(a=1:10, b=1:6, c=5:8)
list_vect2df(L1)

L2 <- list(
  months=setNames(1:12, month.abb),
  numbers=1:6, 
  states=setNames(factor(state.name[1:4]), state.abb[1:4])
)

list_vect2df(L2)

set.seed(10)
cnts <- data.frame(month=month.name,
    matrix(sample(0:2, 36, TRUE), ncol=3))

counts2list(cnts[, -1], cnts[, 1])
df2matrix(cnts)
counts2list(df2matrix(cnts))
counts2list(t(df2matrix(cnts)))

mat <- matrix(1:9, ncol=3)
matrix2long(mat)
matrix2long(mtcars)

## Not run: 
library(qdap)
term <- c("the ", "she", " wh")
(out <- with(raj.act.1,  termco(dialogue, person, term)))
x <- counts(out)

counts2list(x[, -c(1:2)], x[, 1])

## End(Not run)

vect2list(LETTERS[1:10])
vect2list(LETTERS[1:10], numbered.names = TRUE)
x <- setNames(LETTERS[1:4], paste0("Element_", 1:4))
vect2list(x)
vect2list(x, FALSE)
vect2list(x, FALSE, TRUE)

qdapTools documentation built on May 31, 2023, 7:01 p.m.