freq: Frequency Table

View source: R/freq.R

freqR Documentation

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, write = 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.

write

a character string for writing the results into a Excel file naming a file with or without file extension '.xlsx', e.g., "Results.xlsx" or "Results".

check

logical: if TRUE, argument specification is checked.

output

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

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.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

data

list with the input specified in x and group

args

specification of function arguments

result

list with result tables, i.e., freq for absolute frequencies, perc for percentages, and v.perc for valid percentages

Author(s)

Takuya Yanagida takuya.yanagida@univie.ac.at

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

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
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")

## End(Not run)

misty documentation built on Nov. 15, 2023, 1:06 a.m.

Related to freq in misty...