| fxtabs {lazy.tools} | R Documentation |
Perfectly Flat Cross Table by native xtabs
Description
This function makes the result of xtabs perfectly flat.
Usage
fxtabs(
formula = ~.,
data = parent.frame(),
subset,
sparse = FALSE,
na.action,
na.rm = FALSE,
addNA = FALSE,
exclude = if (!addNA) c(NA, NaN),
drop.unused.levels = FALSE,
remove_zeros = 0,
print = 0,
debug = 0
)
Arguments
formula |
a formula object of the form lhs~rhs |
data |
an optional data frame containing the variables in the formula formula. |
subset |
xtabs parameter |
sparse |
xtabs parameter |
na.action |
xtabs parameter |
na.rm |
xtabs parameter |
addNA |
xtabs parameter |
exclude |
xtabs parameter |
drop.unused.levels |
xtabs parameter |
remove_zeros |
= 1 to remove zero frequency cells |
print |
= 1 to print the result |
debug |
= 1 to print the intermediate results |
Details
formula and data parameters function the same way
as native xtab function.
In addtion, those parameters marked as "xtabs parameter",
see stats::xtabs.
The core part of thus function is as follows:
res <- ftable( xtabs( lhs~rhs, data=data ) )
The above returns a matrix when lhs is a matrix.
However, as shown in the examples,
when lhs is a vector or missing, the result is a cross table.
Note that, by adjusting row.vars and col.vars options of
ftable, it is possible to produce a completely flat table.
Value
a matrix of flat contingency table.
Examples
seed=1701
set.seed(seed)
n <- 50
x1 <- sample(1:3,n,replace=TRUE)
x2 <- sample(LETTERS[1:2],n,replace=TRUE)
x3 <- sample(letters[1:2],n,replace=TRUE)
c1 <- sample(c(0:3),n,replace=TRUE)
c2 <- sample(c(0:5),n,replace=TRUE)
data=data.frame(x1,x2,c1,c2)
# 3 by variables
fxtabs( cbind(c1,c2)~x1+x2+x3, data=data)
ftable(xtabs( cbind(c1,c2)~x1+x2+x3, data=data))
fxtabs( c1~x1+x2+x3, data=data)
ftable(xtabs( c1~x1+x2+x3, data=data))
fxtabs( ~x1+x2+x3, data=data)
ftable(xtabs( ~x1+x2+x3, data=data))
# 2 by variables
fxtabs( cbind(c1,c2)~x1+x2, data=data)
ftable(xtabs( cbind(c1,c2)~x1+x2, data=data))
fxtabs( c1~x1+x2, data=data)
ftable(xtabs( c1~x1+x2, data=data))
fxtabs( ~x1+x2, data=data)
ftable(xtabs( ~x1+x2, data=data))
# single by variable
fxtabs( cbind(c1,c2)~x1, data=data)
ftable(xtabs( cbind(c1,c2)~x1, data=data))
fxtabs( c1~x1, data=data)
ftable(xtabs( c1~x1, data=data))
fxtabs( ~x1, data=data)
ftable(xtabs( ~x1, data=data))