Here is my dummy data:
df <- tibble(col1 = c(runif(5, min = 0, max = 5)),
col2 = c("a", "b", "c", "d", "e"),
col3 = c("blue", "blue", "blue", "red", "orange"))
col1 col2 col3
<dbl> <chr> <chr>
1 0.200 a blue
2 1.99 b blue
3 0.663 c blue
4 1.79 d red
5 3.82 e orange
My aim is to add unique identifiers using conditional formatting however, I often have multiple identifiers for the same condition, so they will just overwrite on another.
df %>% mutate(col4 = case_when(
col2 %in% "b" & col3 %in% "blue" ~ "id1",
col2 %in% "b" & col3 %in% "blue" ~ "id2",
TRUE ~ NA_character_
))
col1 col2 col3 col4
<dbl> <chr> <chr> <chr>
1 0.345 a blue NA
2 0.250 b blue id1
3 1.37 c blue NA
4 3.99 d red NA
5 2.14 e orange NA
My desired output simply duplicates the row!
col1 col2 col3 col4
<dbl> <chr> <chr> <chr>
1 0.345 a blue NA
2 0.250 b blue id1
3 0.250 b blue id2
3 1.37 c blue NA
4 3.99 d red NA
5 2.14 e orange NA