When I try to use this code to recode non - mutually exclusive variable categories, it does not work how I would like. For example, the post with the caption, "this post is about both a dog and space" could go in either 'animal_post' or 'other_post'. However, no matter what it always codes it as an 'animal_post' - as seen in code block 1. It only recodes as 'other_post' if I switch the order of the recode instructions itself - as seen in code block 2. Is there a way to recode non-mutually exclusive data without it defaulting to the first option?
#Code block 1
post_caption <- c("This post is about a dog", "This post is about a cat", "This post is about a walrus", "This post is about space", "this post is about both a dog and space")
post_name <- c("dog account", "dog_account", "walrus_account", "space_account", "space_account")
case_when(
grepl(paste(c("dog", "cat", "walrus"), collapse = "|"), post_caption) ~ "animal_post",
grepl(paste(c("space", "rocks", "trees"), collapse = ""), post_caption) & grepl(paste(c("space", "rocks", "trees"), collapse = "|"), post_name)~ "other_post",
TRUE ~ NA_character_
)
[1] "animal_post" "animal_post" "animal_post" "other_post" "animal_post"
#Code block 2
post_caption <- c("This post is about a dog", "This post is about a cat", "This post is about a walrus", "This post is about space", "this post is about both a dog and space")
post_name <- c("dog account", "dog_account", "walrus_account", "space_account", "space_account")
case_when(
grepl(paste(c("space", "rocks", "trees"), collapse = "|"), post_caption) & grepl(paste(c("space", "rocks", "trees"), collapse = "|"), post_name)~ "other_post",
grepl(paste(c("dog", "cat", "walrus"), collapse = "|"), post_caption) ~ "animal_post",
TRUE ~ NA_character_
)
#[1] "animal_post" "animal_post" "animal_post" "other_post" "other_post"