Last active
July 24, 2017 11:14
-
-
Save egouldo/f3893ee2aefe20fa9fa6 to your computer and use it in GitHub Desktop.
Convert contingency table (counts) to cases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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