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(..., data = NULL, 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, append = TRUE, check = TRUE,
     output = TRUE)

Arguments

...

a vector, factor, matrix or data frame. Alternatively, an expression indicating the variable names in data e.g., freq(x1, x2, x3, data = dat). Note that the operators ., +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

data

a data frame when specifying one or more variables in the argument .... Note that the argument is NULL when specifying a vector, factor, matrix or data frame for the argument ....

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 ... is print = "all", while default setting when specifying more than one variable in ... 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 ....

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 ... 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 naming a file for writing the output into either a text file with file extension ".txt" (e.g., "Output.txt") or Excel file with file extension ".xlsx" (e.g., "Output.xlsx"). If the file name does not contain any file extension, an Excel file will be written.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

logical: if TRUE (default), argument specification is checked.

output

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

Details

By default, the function displays the absolute and percentage frequencies when specifying one variable in the argument ..., 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 .... 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

data frame used for the current analysis

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

# Example 1a: Frequency table for 'cyl'
freq(mtcars$cyl)

# Example 1b: Alternative specification using the 'data' argument
freq(cyl, data = mtcars)

# Example 2: Frequency table, values shown in columns
freq(mtcars$cyl, val.col = TRUE)

# Example 3: Frequency table, use 3 digit for displaying percentages
freq(mtcars$cyl, digits = 3)

# Example 4a: Frequency table for 'cyl', 'gear', and 'carb'
freq(mtcars[, c("cyl", "gear", "carb")])

# Example 4b: Alternative specification using the 'data' argument
freq(cyl, gear, carb, data = mtcars)

# Example 5: Frequency table, with percentage frequencies
freq(mtcars[, c("cyl", "gear", "carb")], print = "all")

# Example 6: Frequency table, split output table
freq(mtcars[, c("cyl", "gear", "carb")], split = TRUE)

# Example 7: Frequency table, exclude variables with more than 5 unique values
freq(mtcars, exclude = 5)

## Not run: 
# Example 8a: Write results into a text file
freq(mtcars[, c("cyl", "gear", "carb")], split = TRUE, write = "Frequencies.txt")

# Example 8b: Write results into an Excel file
freq(mtcars[, c("cyl", "gear", "carb")], split = TRUE, write = "Frequencies.xlsx")

result <- freq(mtcars[, c("cyl", "gear", "carb")], split = TRUE, output = FALSE)
write.result(result, "Frequencies.xlsx")

## End(Not run)

misty documentation built on Oct. 24, 2024, 5:10 p.m.

Related to freq in misty...