freq_function function

Frequency Tables with Percentage and Odds Ratios

Frequency Tables with Percentage and Odds Ratios

A method for making 1- and 2-way frequency tables with percentages and odds ratios.

freq_function( normaldata, var1, var2 = NULL, by_vars = NULL, include_NA = FALSE, values_to_remove = NULL, weightvar = NULL, textvar = NULL, number_decimals = 2, output = c("all", "numeric", "col", "colw", "row", "roww", "total", "totalw"), chisquare = FALSE )

Arguments

  • normaldata: A data frame or data frame extension (e.g. a tibble).

  • var1: A character string naming the first variable to get frequencies.

  • var2: An optional character naming the second variable to get frequencies. If NULL (standard) a 1-way frequency table of only var1 is created, and if var2 is specified a 2-way table is returned.

  • by_vars: An optional character vector naming variables in normal_data

    to stratify the calculations and output by. That is, ALL calculations will be made within the combinations of variables in the vector, hence it's possible to get N and % for many groups in one go.

  • include_NA: A logical. If FALSE (standard) missing variables (NA's) will be removed from var1 and var2. Any missing values in by_vars

    will not be removed. If TRUE all missing values will be included in calculations and the output.

  • values_to_remove: An optional character vector. When specified all values from var1 and var2 found in values_to_remove will be removed from the calculations and output.

  • weightvar: An optional character naming a column in normaldata with numeric weights for each observation. If NULL (standard) all observations have weight 1.

  • textvar: An optional character. When specified textvar is added to the resulting table as a comment. When NULL (standard) no such text addition is made.

  • number_decimals: A numeric indicating the number of decimals to show on percentages and weighted frequencies in the combined frequency and percent variables.

  • output: A character indicating the output type wanted:

    • "all" - will give ALL output from tables. In many cases unnecessary and hard to get an overview of. This is set as the standard.
    • "numeric" - will give frequencies and percents as numeric variables only, thus the number_decimals option is not in effect. This option might be useful when making figures/graphs.
    • "col" - will only give unweighted number of observations and weighted column percent (if weights are used, otherwise unweighted)
    • "colw" - will only give weighted number of observations and weighted column percent (if weights are used, otherwise unweighted)
    • "row"- will only give unweighted number of observations and weighted row percent (if weights are used, otherwise unweighted). Only works in two-way tables (var2 is specified)
    • "roww" - will only give weighted number of oberservations and weighted column percent (if weights are used, otherwise unweighted). Only works in two-way tables (var2 is specified)
    • "total" - will only give unweighted number of observations and weighted percent of the total (if weights are used, otherwise unweighted). Only works in two-way tables (var2 is specified)
    • "totalw" - will only give weighted number of observations and weighted percent of the total (if weights are used, otherwise unweighted). Only works in two-way tables (var2 is specified)
    • Any other text will give the default ("all")
  • chisquare: A logical. FALSE (standard) will not calculate p-value for the chi-square test for two-way tables (var2 is specified). If TRUE, the table will include the chi-square p-value as well as the chi-square statistic and the corresponding degrees of freedom. It will be included in the output whichever output option have been specified. No chi-square test is performed or included in one-way tables (var2 is unspecified)

Returns

A frequency table as a data frame object.

Examples

data("starwars", package = "dplyr") test_table1 <- freq_function( starwars, var1 = "homeworld" ) test_table2 <- freq_function( starwars, var1 = "sex", var2 = "eye_color", output = "total" ) test_table3 <- freq_function( starwars, var1 = "hair_color", var2 = "skin_color", by_vars = "gender", output = "col", number_decimals = 5 )

See Also

freq_function_repeated() to to get frequencies for multiple variables in one go.

Author(s)

ASO

  • Maintainer: Kim Daniel Jakobsen
  • License: MIT + file LICENSE
  • Last published: 2024-02-26

Useful links