We have quite a few table functions in R, all of them solving a specific problem.
library(DescTools)
# Let's define a 3-dim table
# source: http://www.math.wpi.edu/saspdf/stat/chap28.pdf, page 1248
school <- as.table(array(c(35,29, 14,27, 32,10, 53,23), dim=c(2,2,2),
dimnames=list(enrollment=c("yes","no"),
internship=c("yes","no"), gender=c("boys","girls"))))
school
# the percentages
prop.table(school)
# printed as flat table
ftable(school, row.vars=c(3,2))
# aggregate and layout
xtabs(Freq ~ enrollment + gender, data=school)
# and the inverse Operation
tab <- xtabs(Freq ~ enrollment + gender, data=school)
d.frm <- as.data.frame.table(tab)
# factor form with frequencies
data.frame(school)
# get the raw data back in a data.frame
head(Untable(school))
# describe with bells and whistles
Desc(xtabs(Freq ~ internship + enrollment, data=school) )
Assocs(xtabs(Freq ~ internship + enrollment, data=school) )
# how to make a data.frame out of a table
tab <- table(d.pizza$driver, d.pizza$area)
d.frm <- data.frame(unclass(tab))
# or even simpler with base function:
d.frm <- as.data.frame.matrix(tab)
# combine several arrays:
abs <- apply(Titanic, 1:length(dim(Titanic)), formatC, width=3)
rel <- apply(prop.table(Titanic), 1:length(dim(Titanic)), FormatFix, after=3)
ftable(abind(abs, rel))
Dienstag, 27. August 2013
Abonnieren
Posts (Atom)