This is for a guessing game and these values will change. I am trying to find / write a function that will replace all those if_else statements to improve this code. So I don't have to copy and paste into the mutate.
library(dplyr)
df <- tribble(
~your_name, ~who_did_you_get, ~barbara, ~brent, ~emma, ~joe, ~liis, ~shane,
"liis", "shane", "brent", "emma", "joe", "shane", "barbara", "barbara",
"barbara", "brent", "shane", "liis", "joe", "emma", "brent", "barbara",
"brent", "emma", "shane", "liis", "joe", "emma", "brent", "barbara",
"emma", "joe", "brent", "emma", "shane", "barbara", "barbara", "emma",
"joe", "liis", "shane", "liis", "joe", "emma", "brent", "barbara",
"shane", "barbara", "emma", "emma", "liis", "shane", "barbara", "joe"
)
paste(df$key, collapse = ", ") # paste this output to make the following
df %>%
mutate(barbara_q = if_else(barbara == "brent" , 1, 0),
brent_q = if_else(brent == "emma" , 1, 0),
emma_q = if_else(emma == "joe" , 1, 0),
joe_q = if_else(joe == "liis" , 1, 0),
liis_q = if_else(liis == "shane" , 1, 0),
shane_q = if_else(shane == "barbara" , 1, 0),
) %>%
mutate(score = rowSums(select(., barbara_q:shane_q))) %>%
arrange(desc(score))