Hi, everyone!
I'm trying to create a function to summarize the number of studies that received a particular value in columns in a df. In the following example, the first column is for study names, and Item1:Item3 are variables. In particular, Item1:Item3 contain codes that a coding team assigned to information in a study. In the function, I want to be able to plug in different dfs; each df will always have the same "Study" column, but the number of variables might change from one df to the next.
Can someone help me turn this into a function? Thank you! (Thanks to woodman for getting me going here.)
df <- tibble(
Study = c( rep("Wash_2009", 5),
rep("Zoey_2001", 12),
rep("Jane_1999", 10),
rep("Todd_1993", 15),
rep("Coco_2019", 5),
rep("Xena_2016", 3) ),
Item1 = sample( c(1, 2, 3, 4, 5, "NS", "OT"), 50, T),
Item2 = sample( c(1, 2, 3, 4, 5, "NS", "OT"), 50, T),
Item3 = sample( c(1, 2, 3, 4, 5, "NS", "OT"), 50, T)
)
Item1 <- df %>%
group_by(Study) %>%
count(Item1) %>%
group_by(Item1) %>%
summarise(Studies = n())
Item2 <- df %>%
group_by(Study) %>%
count(Item2) %>%
group_by(Item2) %>%
summarise(Studies = n())
Item3 <- df %>%
group_by(Study) %>%
count(Item3) %>%
group_by(Item3) %>%
summarise(Studies = n())
Item1
Item2
Item3