Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Expand table of counts to a dataframe

Given a table of counts specified in ‘dat’ I would like to create a dataframe with 3 columns (race, grp and outcome) and 206 rows. The variable outcome would be 1 if for ascertained, and 0 if ‘missed’.

dat <- structure(list(race = structure(c(1L, 2L, 1L, 2L), levels = c("black", 
"nonblack"), class = "factor"), grp = structure(c(1L, 1L, 2L, 
2L), levels = c("hbpm", "uc"), class = "factor"), ascertained = c(63, 
32, 24, 21), missed = c(5, 3, 49, 9), total = c(68, 35, 73, 30
)), class = "data.frame", row.names = c(NA, -4L))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For each row set race in the output to that race, grp in the output to that group and then generate the appropriate number of 1s and 0s for outcome. The result is 206 x 3.

library(dplyr)

dat %>%
  rowwise %>%
  summarize(race = race, grp = grp, outcome = rep(1:0, c(ascertained, missed)))

In the example data there are no duplicate race/grp and if that is true in general then it can alternately be written as::

dat %>%
  group_by(race, grp) %>%
  summarize(outcome = rep(1:0, c(ascertained, missed)), .groups = "drop")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading