I know there is a better way or some type of loop to do this, but what would be the best way to do the mutate/case_when step below instead of doing so many mutate steps. Essentially, I'm trying to add 168 columns to the "new" dataset, one for each "type", where "group1" will always be "A", and then again where "group2" will always be "C". There are 84 unique types.
The only shortcoming of the code is that the case_when code produces NAs where unaddressed combinations occur. Avoid with
suppressPackageStartupMessages({
library(dplyr)
})
set.seed(42)
n <- 84
datA <- data.frame(id=1:n,
type=factor(paste("type", 1:n)),
group1=sample(rep(LETTERS[1:2], n/2)),
group2=sample(rep(LETTERS[3:4], n/2)))
datB <- data.frame(id=1:n,
type=factor(paste("type", 1:n)),
group1=sample(rep(LETTERS[1:2], n/2)),
group2=sample(rep(LETTERS[3:4], n/2)))
dat <- rbind(datA, datB)
new <- dat %>%
mutate(type1Group1 = ifelse(type == "type 1" & group1 == "A",TRUE,FALSE))
# repeat for other tests
head(new)
#> id type group1 group2 type1Group1
#> 1 1 type 1 A D TRUE
#> 2 2 type 2 A D FALSE
#> 3 3 type 3 A D FALSE
#> 4 4 type 4 B C FALSE
#> 5 5 type 5 B C FALSE
#> 6 6 type 6 B C FALSE