ftable2dt: Convert an 'ftable' or an 'array' Object to a 'data.table'

Description Usage Arguments Value Note Author(s) References Examples

View source: R/ftable2dt.R

Description

While convenient methods exist for converting tables and other objects to data.tabless, such methods do not exist for converting an ftable to a data.table. An ftable is essentially a matrix with attributes for the rows and columns, which can be nested.

Usage

1
ftable2dt(inarray, direction = "wide")

Arguments

inarray

The input ftable or array.

direction

Should the reslut be "wide" (with multiple measurement. columns) or "long" (with a single measurement column)? Defaults to "wide".

Value

A data.table

Note

If the array has no dimnames, names would be added using the provideDimnames function. Defaults to reshape2::melt() if the input is a simple matrix and not a multidimensional array.

Author(s)

Ananda Mahto

References

http://stackoverflow.com/a/11143126/1270695

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
x <- ftable(Titanic, row.vars = 1:3)
x
ftable2dt(x)
ftable2dt(x, direction = "long")

## Not run: 
dims <- c(2, 1, 2, 3, 2)
set.seed(1)
M <- `dim<-`(sample(100, prod(dims), TRUE), dims)
N <- O <- `dimnames<-`(M, lapply(dims, function(x) 
                                 c(letters, LETTERS)[seq_len(x)]))
names(attributes(O)$dimnames) <- c("first", "second", "third", 
                                   "fourth", "fifth")

ftable2dt(M)
ftable2dt(N)
ftable2dt(O)
ftable2dt(M, "long")
ftable2dt(N, "long")
ftable2dt(O, "long")

## End(Not run)

mrdwab/SOfun documentation built on June 20, 2020, 6:15 p.m.