View source: R/counts_to_cases.R
counts_to_cases | R Documentation |
Takes a data frame or a table object in R with counts and converts it to a data frame with the cases.
counts_to_cases(input_table, countcol = "Freq")
input_table |
A table object |
countcol |
the name of the column containing the counts (default = "Freq") |
A data frame
cookbook-r.com, Converting_between_data_frames_and_contingency_tables cookbook-r.com countstocases function
#### 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.