freq_function_repeated function

Wrapper for freq_function() to get frequencies for many variables in one go.

Wrapper for freq_function() to get frequencies for many variables in one go.

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

freq_function_repeated( 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 vector with the names of the first variable to get frequencies from for each frequency table.

  • var2: An optional character naming the second variable to get frequencies. If NULL (standard) 1-way frequency tables of only variables in var1 are created, and if var2 is specified 2-way tables are 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

Multiple frequency tables stored in a data frame object.

Examples

# Examples data("starwars", package = "dplyr") test_table1 <- freq_function_repeated( starwars, var1 = c("sex","homeworld","eye_color"), include_NA = TRUE ) test_table2 <- freq_function_repeated( starwars, var1 = c("homeworld","eye_color","skin_color"), var2 = "sex", output = "col", number_decimals = 3 ) test_table3 <- freq_function_repeated( starwars, var1 = c("homeworld","eye_color","skin_color"), var2 = "sex", by_vars = c("gender"), output = "row" )

See Also

freq_function() for the function that creates frequency tables for single variables.

Author(s)

ASO

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

Useful links