I'm trying a build a variable (Three_CM2) based on the variable CODE_UAI such that if a value in CODE_UAI is duplicated 3 times then 'Three_CM2' equals 3. So far, I've come up with this which does not work. I feel like I'm on the right track but need the extra push.
Thanks!
Ech_final_nom_BSA %>%
mutate(Three_CM2 = case_when(count(CODE_UAI) == 3 ~ 1, T ~ 0))
CodePudding user response:
Does this work:
library(dplyr)
Ech_final_nom_BSA <- data.frame(CODE_UAI = c(1,1,1,2,3,4,4,4))
Ech_final_nom_BSA
CODE_UAI
1 1
2 1
3 1
4 2
5 3
6 4
7 4
8 4
Ech_final_nom_BSA %>% group_by(CODE_UAI) %>% mutate(Three_CM2 = case_when(n() == 3 ~ 1, T ~ 0))
# A tibble: 8 x 2
# Groups: CODE_UAI [4]
CODE_UAI Three_CM2
<dbl> <dbl>
1 1 1
2 1 1
3 1 1
4 2 0
5 3 0
6 4 1
7 4 1
8 4 1
CodePudding user response:
Using base R
Ech_final_nom_BSA$Three_CM2 <- with(Ech_final_nom_BSA, (ave(CODE_UAI, CODE_UAI, FUN = length) == 3))