dat <- data.frame(x1 = c(3, 3, 2, 3, 2, 3, 3, 2, 1, -99),
x2 = c(2, 2, 1, 3, 1, 1, 3, 3, 2, 2),
y1 = c(1, 4, NA, 5, 2, 4, 3, 5, NA, 1),
y2 = c(2, 3, 4, 3, NA, 4, 2, 3, 4, 5),
z = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
# Frequency table for one variable
freq(dat$x1)
# Frequency table for one variable,
# values shown in columns
freq(dat$x1, val.col = TRUE)
# Frequency table for one variable,
# convert value -99 into NA
freq(dat$x1, as.na = -99)
# Frequency table for one variable
# use 3 digit for displaying percentages
freq(dat$x1, digits = 3)
# Frequency table for more than one variable
freq(dat[, c("x1", "x2", "y1", "y2")])
# Frequency table for more than one variable,
# values shown in columns
freq(dat[, c("x1", "x2", "y1", "y2")], val.col = TRUE)
# Frequency table for more than one variable,
# with percentage frequencies
freq(dat[, c("x1", "x2", "y1", "y2")], print = "all")
# Frequency table for more than one variable,
# with percentage frequencies, values shown in columns
freq(dat[, c("x1", "x2", "y1", "y2")], print = "all", val.col = TRUE)
# Frequency table for more than one variable,
# split output table
freq(dat[, c("x1", "x2", "y1", "y2")], split = TRUE)
# Frequency table for more than one variable,
# exclude variables with more than 5 unique values
freq(dat, exclude = 5)
# Frequency table for a factor
freq(factor(c("a", "a", "b", "c", "b")))
# Frequency table for one variable,
# do not use labels of the factor levels
freq(factor(c("a", "a", "b", "c", "b")), labels = FALSE)
if (FALSE) {
# Write Results into a Excel file
freq(dat[, c("x1", "x2", "y1", "y2")], split = TRUE, write = "Frequencies.xlsx")
result <- freq(dat[, c("x1", "x2", "y1", "y2")], split = TRUE, output = FALSE)
write.result(result, "Frequencies.xlsx")
}
Run the code above in your browser using DataLab