counts_to_cases: Convert from a table or data frame of counts to data frame of...

View source: R/counts_to_cases.R

counts_to_casesR Documentation

Convert from a table or data frame of counts to data frame of cases.

Description

Takes a data frame or a table object in R with counts and converts it to a data frame with the cases.

Usage

counts_to_cases(input_table, countcol = "Freq")

Arguments

input_table

A table object

countcol

the name of the column containing the counts (default = "Freq")

Value

A data frame

References

cookbook-r.com, Converting_between_data_frames_and_contingency_tables cookbook-r.com countstocases function

Examples

#### Example 1 --------------------------------
library(vcd)
class(SexualFun)
counts_to_cases(input_table = SexualFun,
                countcol = "Freq")

#### Example 2 --------------------------------
phs <- matrix(c(189, 10845, 104, 10933), byrow = TRUE, ncol = 2)
dimnames(phs) <- list(Group = c("Placebo", "Aspirin"), MI = c("Yes", "No"))
phs
class(phs)
counts_to_cases(input_table = phs,
                countcol = "Freq")

#### Example 3 --------------------------------
# This last example uses data from a frequency tablethat might be entered into
# SAS. This serves as a helpful reminder to myself.
library(dplyr)
library(tibble)
library(irr)  # For the kappa2 function.

rate <- tibble::tribble(
  ~rater1, ~rater2, ~freq,
     "Lo",    "Lo",     0,
     "Lo",   "Med",     0,
     "Lo",    "Hi",     0,
    "Med",    "Lo",     5,
    "Med",   "Med",    16,
    "Med",    "Hi",     3,
     "Hi",    "Lo",     8,
     "Hi",   "Med",    12,
     "Hi",    "Hi",    28
  )

rate <- tibble::tribble(
  ~rater1, ~rater2, ~freq,
     "Lo",    "Lo",     0,
     "Lo",   "Med",     0,
     "Lo",    "Hi",     0,
    "Med",    "Lo",     5,
    "Med",   "Med",    16,
    "Med",    "Hi",     3,
     "Hi",    "Lo",     8,
     "Hi",   "Med",    12,
     "Hi",    "Hi",    28
  )

(rate <- xtabs(freq ~ rater1 + rater2, data = rate))

(rate <- lamisc::counts_to_cases(input_table = rate) %>%
    mutate_all(.tbl = .,
             .funs = funs(factor(.,
                                 levels = c("Lo", "Med", "Hi"),
                                 labels = c("Lo", "Med", "Hi")))))

irr::kappa2(rate)


emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.