Home > Back-end >  Overwrite variables if condition is met, else keep existing values R
Overwrite variables if condition is met, else keep existing values R

Time:05-07

I have a data frame

df<-data.frame(Name=c('H001', 'H002', 'H003', 'H004', 'H005', 'H006',
                      'H007', 'H008', 'H009', 'H010'),
               Var1=c(1:10),
               Var2=c(4,6,7,2,3,8,9,3,2,10),
               Var3=c(1,0.7,0.5,0.74,0.84,0.8,0.13,0.7,0.34,0.4))

I want to reduce the original value in column Var3 if it is above a given threshold, but keep the original value if it is below said threshold. I have tried this, but this induces NAs:

df %>% 
  mutate(Var3 = case_when(
    Name %in% c("H001", "H002") & Var3 >0.32 ~ 0.32,
    Name %in% c("H003", "H004") & Var3 >0.22 ~ 0.32,
    Name %in% c("H005", "H006") & Var3 >0.15 ~ 0.15,
    Name %in% c("H007", "H008") & Var3 >0.18 ~ 0.18,
    Name %in% c("H009", "H010") & Var3 >0.42 ~ 0.32,
  ))

Is there a way to retain the original value instead of NA? Thanks in advance

CodePudding user response:

You can add a final else statement at the end of your case_when, so that if none of the other conditions are met, then it will just return Var3 for a given row. By default, it will return NA if none of the other conditions are met.

df %>% 
  mutate(Var3 = case_when(
    Name %in% c("H001", "H002") & Var3 >0.32 ~ 0.32,
    Name %in% c("H003", "H004") & Var3 >0.22 ~ 0.32,
    Name %in% c("H005", "H006") & Var3 >0.15 ~ 0.15,
    Name %in% c("H007", "H008") & Var3 >0.18 ~ 0.18,
    Name %in% c("H009", "H010") & Var3 >0.42 ~ 0.32,
    TRUE ~ Var3
  ))

Output

   Name Var1 Var2 Var3
1  H001    1    4 0.32
2  H002    2    6 0.32
3  H003    3    7 0.32
4  H004    4    2 0.32
5  H005    5    3 0.15
6  H006    6    8 0.15
7  H007    7    9 0.13
8  H008    8    3 0.18
9  H009    9    2 0.34
10 H010   10   10 0.40
  • Related