I have a data frame:
ID <- c(1:10)
act1_144 <- c("Bedroom", "Bedroom", "Bedroom", "Lounge", "Kitchen", "Bedroom", "Bedroom", "Bedroom", "Lounge", "Kitchen")
wher_144 <- c("Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home", "Home")
data <- data.frame(ID, act1_144, wher_144)
and I am using mutate to make a new column based on the conditions of two other columns, e.g.
library(dplyr)
data <- data %>%
mutate(new_144 = case_when(
act1_144 == "Bedroom" & wher_144 == "Home" ~ "Bedroom",
act1_144 == "Kitchen" & wher_144 == "Home" ~ "Kitchen",
act1_144 == "Lounge" & wher_144 == "Home" ~ "Lounge",
act1_144 == "Bathroom" & wher_144 == "Home" ~ "Bathroom"))
My real data frame has the variables act1_1 - act1_144 and wher_1 - wher_144. I'd like to put the above code into a function to loop through all matching combinations of act1_** and wher_**, as in the example above, without having to rerun the code 144 times.
Is this possible? Thank you in advance