Skip to content

Instantly share code, notes, and snippets.

@egouldo
Last active July 24, 2017 11:14
Show Gist options
  • Save egouldo/f3893ee2aefe20fa9fa6 to your computer and use it in GitHub Desktop.
Save egouldo/f3893ee2aefe20fa9fa6 to your computer and use it in GitHub Desktop.
Convert contingency table (counts) to cases
# Source: http://www.cookbook-r.com/Manipulating_data/Converting_between_data_frames_and_contingency_tables/#countstocases-function
# Convert from data frame of counts to data frame of cases.
# `countcol` is the name of the column containing the counts
countsToCases <- function(x, countcol = "Freq") {
# Get the row indices to pull from x
idx <- rep.int(seq_len(nrow(x)), x[[countcol]])
# Drop count column
x[[countcol]] <- NULL
# Get the rows from x
x[idx, ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment