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.

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.

References

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

Author

Ananda Mahto

Examples

x <- ftable(Titanic, row.vars = 1:3) x
#> Survived No Yes #> Class Sex Age #> 1st Male Child 0 5 #> Adult 118 57 #> Female Child 0 1 #> Adult 4 140 #> 2nd Male Child 0 11 #> Adult 154 14 #> Female Child 0 13 #> Adult 13 80 #> 3rd Male Child 35 13 #> Adult 387 75 #> Female Child 17 14 #> Adult 89 76 #> Crew Male Child 0 0 #> Adult 670 192 #> Female Child 0 0 #> Adult 3 20
ftable2dt(x)
#> Class Sex Age No Yes #> 1: 1st Female Adult 4 140 #> 2: 1st Female Child 0 1 #> 3: 1st Male Adult 118 57 #> 4: 1st Male Child 0 5 #> 5: 2nd Female Adult 13 80 #> 6: 2nd Female Child 0 13 #> 7: 2nd Male Adult 154 14 #> 8: 2nd Male Child 0 11 #> 9: 3rd Female Adult 89 76 #> 10: 3rd Female Child 17 14 #> 11: 3rd Male Adult 387 75 #> 12: 3rd Male Child 35 13 #> 13: Crew Female Adult 3 20 #> 14: Crew Female Child 0 0 #> 15: Crew Male Adult 670 192 #> 16: Crew Male Child 0 0
ftable2dt(x, direction = "long")
#> Class Sex Age Survived value #> 1: 1st Male Child No 0 #> 2: 1st Male Adult No 118 #> 3: 1st Female Child No 0 #> 4: 1st Female Adult No 4 #> 5: 2nd Male Child No 0 #> 6: 2nd Male Adult No 154 #> 7: 2nd Female Child No 0 #> 8: 2nd Female Adult No 13 #> 9: 3rd Male Child No 35 #> 10: 3rd Male Adult No 387 #> 11: 3rd Female Child No 17 #> 12: 3rd Female Adult No 89 #> 13: Crew Male Child No 0 #> 14: Crew Male Adult No 670 #> 15: Crew Female Child No 0 #> 16: Crew Female Adult No 3 #> 17: 1st Male Child Yes 5 #> 18: 1st Male Adult Yes 57 #> 19: 1st Female Child Yes 1 #> 20: 1st Female Adult Yes 140 #> 21: 2nd Male Child Yes 11 #> 22: 2nd Male Adult Yes 14 #> 23: 2nd Female Child Yes 13 #> 24: 2nd Female Adult Yes 80 #> 25: 3rd Male Child Yes 13 #> 26: 3rd Male Adult Yes 75 #> 27: 3rd Female Child Yes 14 #> 28: 3rd Female Adult Yes 76 #> 29: Crew Male Child Yes 0 #> 30: Crew Male Adult Yes 192 #> 31: Crew Female Child Yes 0 #> 32: Crew Female Adult Yes 20 #> Class Sex Age Survived value
if (FALSE) { 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") }