I'm trying to convert one df to another format.
Starting df:
df <- data.frame(id = c("1", "2", "3","4", "5", "6", "7", "8", "9", "10"),
criteria_A = c("present", "present", "absent", "absent", "absent", "present", "absent", "present", "absent", "present"),
criteria_B =c("absent", "absent", "present", "absent", "absent", "present", "absent", "absent", "present", "present"))
I want to count each criterion by present/absent and retabulate it as such:
df2 <- data.frame(criteria = c("criteria_A", "criteria_A", "criteria_B", "criteria_B"),
count = c("5", "5", "4", "6"),
status = c("present", "absent", "present", "absent"))
I thought about getting the count this way per criterion:
library(dplyr)
tmp1 <- df %>% group_by(criteria_A) %>% count() %>% mutate(criteria="criteria_A")
tmp1 <- tmp1 %>% rename(criteria_A=status)
tmp2 <- df %>% group_by(criteria_B) %>% count() %>% mutate(criteria="criteria_B")
tmp2 <- tmp2 %>% rename(criteria_B=status)
I figured I could then merge the outputs vertically. This is not an efficient or clever way to do it when in reality I have hundreds of criteria...
I'm sure there's an elegant solution I'm not clever enough to figure out!
Any help as always would be gratefully received.