Description Usage Arguments Author(s) References See Also Examples
The table2df function takes an object of "class"
table, ftable, and xtabs and
converts them to data.frames, while retaining as many of the
name details as possible.
1 |
mytable |
The |
as.multitable |
Logical; defaults to
|
direction |
Can be either
|
Ananda Mahto
The expand.grid method for remaking the columns from an
ftable was described by Kohske at
http://stackoverflow.com/a/6463137/1270695.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | # Make up some data:
set.seed(1)
handedness <- data.frame(
gender = sample(c("Female", "Male", "Unknown"), 200, replace = TRUE),
handedness = sample(c("Right", "Left", "Ambidextrous"),
200, replace = TRUE, prob = c(.7, .2, .1)),
fav.col = sample(c("Red", "Orange", "Yellow", "Green", "Blue",
"Indigo", "Violet", "Black", "White"),
200, replace = TRUE),
fav.shape = sample(c("Triangle", "Circle", "Square", "Pentagon", "Hexagon",
"Oval", "Octagon", "Rhombus", "Trapezoid"),
200, replace = TRUE),
computer = sample(c("Win", "Mac", "Lin"), 200, replace = TRUE,
prob = c(.5, .25, .25)))
# Preview the data
list(head(handedness), tail(handedness))
# A very basic table
HT1 <- with(handedness, table(gender, handedness))
HT1
table2df(HT1)
#'table2df(HT1, direction = "long")
# Another basic table
HT2 <- with(handedness, table(fav.col, computer))
HT2
table2df(HT2)
# This will create multiple tables, one for each possible computer value
HT3 <- with(handedness, table(gender, fav.col, computer))
HT3
# Default settings
table2df(HT3)
# As a list of data.frames
table2df(HT3, as.multitable = TRUE)
# As above, but with the output in long format
# Only showing the first three lines of each data.frame
lapply(table2df(HT3, as.multitable = TRUE, direction = "long"), head, 3)
# Applied to an ftable
HT4 <- ftable(handedness,
col.vars="fav.col",
row.vars=c("gender", "computer"))
HT4
table2df(HT4)
# Applied to a single-row table
table2df(xtabs(breaks ~ wool, warpbreaks))
## ======================================= ##
## ========== OTHER EXAMPLES ============= ##
## Not run:
table2df(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph))
table2df(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph),
direction = "long")
table2df(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph),
as.multitable = TRUE, direction = "long")
table2df(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph),
as.multitable = TRUE, direction = "wide")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.