Learn R Programming

misty (version 0.4.5)

freq: Frequency Table

Description

This function computes a frequency table with absolute and percentage frequencies for one or more than one variable.

Usage

freq(x, print = c("no", "all", "perc", "v.perc"), freq = TRUE, split = FALSE,
     labels = TRUE, val.col = FALSE, round = 3, exclude = 15, digits = 2,
     as.na = NULL, check = TRUE, output = TRUE)

Arguments

x

a vector, factor, matrix or data frame.

print

a character string indicating which percentage(s) to be printed on the console, i.e., no percentages ("no"), all percentages ("all"), percentage frequencies ("print"), and valid percentage frequencies ("v.perc"). Default setting when specifying one variable in x is print = "all", while default setting when specifying more than one variable in x is print = "no" unless split = TRUE.

freq

logical: if TRUE (default), absolute frequencies will be shown on the console.

split

logical: if TRUE, output table is split by variables when specifying more than one variable in x.

labels

logical: if TRUE (default), labels for the factor levels will be used.

val.col

logical: if TRUE, values are shown in the columns, variables in the rows.

round

an integer value indicating the number of decimal places to be used for rounding numeric variables.

exclude

an integer value indicating the maximum number of unique values for variables to be included in the analysis when specifying more than one variable in x i.e., variables with the number of unique values exceeding exclude will be excluded from the analysis. It is also possible to specify exclude = FALSE to include all variables in the analysis.

digits

an integer value indicating the number of decimal places 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 shown on the console.

Value

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

Details

By default, the function displays the absolute and percentage frequencies when specifying one variable in the argument x, while the function displays only the absolute frequencies when more than one variable is specified. The function displays valid percentage frequencies only in the presence of missing values and excludes variables with all values missing from the analysis. Note that it is possible to mix numeric variables, factors, and character variables in the data frame specified in the argument x. By default, numeric variables are rounded to three digits before computing the frequency table.

References

Becker, R. A., Chambers, J. M., & Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.

See Also

write.result, crosstab, descript, multilevel.descript, na.descript.

Examples

Run this code
# NOT RUN {
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)

# }
# NOT RUN {
# Write Results into a Excel file
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