Learn R Programming

misty (version 0.4.5)

crosstab: Cross Tabulation

Description

This function creates a two-way and three-way cross tabulation with absolute frequencies and row-wise, column-wise and total percentages.

Usage

crosstab(x, print = c("no", "all", "row", "col", "total"), freq = TRUE,
         split = FALSE, na.omit = TRUE, digits = 2, as.na = NULL,
         check = TRUE, output = TRUE)

Arguments

x

a matrix or data frame with two or three columns.

print

a character string or character vector indicating which percentage(s) to be printed on the console, i.e., no percentages ("no") (default), all percentages ("all"), row-wise percentages ("row"), column-wise percentages ("col"), and total percentages ("total").

freq

logical: if TRUE, absolute frequencies will be included in the cross tabulation.

split

logical: if TRUE, output table is split in absolute frequencies and percentage(s).

na.omit

logical: if TRUE, incomplete cases are removed before conducting the analysis (i.e., listwise deletion).

digits

an integer indicating the number of decimal places digits to be used for displaying percentages.

as.na

a numeric vector indicating user-defined missing values, i.e. these values are converted to NA before conducting the analysis.

check

logical: if TRUE, argument specification is checked.

output

logical: if TRUE, output is printed on the console.

Value

Returns an object of class misty.object, which is a list with following entries: function call (call), matrix or data frame specified in x (data), specification of function arguments (args), and list with results (result).

References

Rasch, D., Kubinger, K. D., & Yanagida, T. (2011). Statistics in psychology - Using R and SPSS. John Wiley & Sons.

Examples

Run this code
# NOT RUN {
dat <- data.frame(x1 = c(1, 2, 2, 1, 1, 2, 2, 1, 1, 2),
                  x2 = c(1, 2, 2, 1, 2, 1, 1, 1, 2, 1),
                  x3 = c(-99, 2, 1, 1, 1, 2, 2, 2, 2, 1))

# Cross Tabulation for x1 and x2
crosstab(dat[, c("x1", "x2")])

# Cross Tabulation for x1 and x2
# print all percentages
crosstab(dat[, c("x1", "x2")], print = "all")

# Cross Tabulation for x1 and x2
# print row-wise percentages
crosstab(dat[, c("x1", "x2")], print = "row")

# Cross Tabulation for x1 and x2
# print col-wise percentages
crosstab(dat[, c("x1", "x2")], print = "col")

# Cross Tabulation x1 and x2
# print total percentages
crosstab(dat[, c("x1", "x2")], print = "total")

# Cross Tabulation for x1 and x2
# print all percentages, split output table
crosstab(dat[, c("x1", "x2")], print = "all", split = TRUE)

# Cross Tabulation for x1 and x3
# do not apply listwise deletion, convert value -99 to NA
crosstab(dat[, c("x1", "x3")], na.omit = FALSE, as.na = -99)

# Cross Tabulation for x1 and x3
# print all percentages, do not apply listwise deletion, convert value -99 to NA
crosstab(dat[, c("x1", "x3")], print = "all", na.omit = FALSE, as.na = -99)

# Cross Tabulation for x1, x2, and x3
crosstab(dat[, c("x1", "x2", "x3")])

# Cross Tabulation for x1, x2, and x3
# print all percentages
crosstab(dat[, c("x1", "x2", "x3")], print = "all")

# Cross Tabulation for x1, x2, and x3
# print all percentages, split output table
crosstab(dat[, c("x1", "x2", "x3")], print = "all", split = TRUE)

# }
# NOT RUN {
# Write Results into a Excel file
result <- crosstab(dat[, c("x1", "x2")], print = "all", output = FALSE)
write.result(result, "Crosstab.xlsx") 
# }

Run the code above in your browser using DataLab