I have a data frame that looks like this
library(tidyverse)
df3 <- tibble(col1 = c("apple",rep("banana",3)),
col2 = c("aple", "banan","bananb","banat"),
count_col1 = c(1,4,4,4),
count_col2 = c(4,1,1,1))
df3
#> # A tibble: 4 × 4
#> col1 col2 count_col1 count_col2
#> <chr> <chr> <dbl> <dbl>
#> 1 apple aple 1 4
#> 2 banana banan 4 1
#> 3 banana bananb 4 1
#> 4 banana banat 4 1
Created on 2022-02-17 by the reprex package (v2.0.1)
I want to group_by col1 and when count_col2 > count_col1 the value of col1 to be converted to NA,
and when count_col1 > count_col2 then the value of col2 to be converted to NA.
I want my data to look like this
#> # A tibble: 4 × 4
#> col1 col2 count_col1 count_col2
#> <chr> <chr> <dbl> <dbl>
#> 1 NA aple 1 4
#> 2 banana NA 4 1
#> 3 banana NA 4 1
#> 4 banana NA 4 1
I am not sure if this can be achieved by mutate(case_when...) i fail so far
df3 %>%
group_by(col1) %>%
mutate(case_when(count_col2 > count_col1 ~ col1==NA,
count_col1 > count_col2 ~ col2==NA ))