Home > Mobile >  Each value greater than x, assign for it a string in a new column
Each value greater than x, assign for it a string in a new column

Time:06-26

Say I have a data frame with one column A, something like this

head(df)

    A
1  200
2  230
3  400
4  638
5  502
6  387

I want to add a new column. For each value equal or greater than 400 in A, there would be nonNormal. otherwise, there would be Normal, something like this

    A     B
1  200  Normal
2  230  Normal
3  400  nonNormal
4  638  nonNormal
5  502  nonNormal
6  387  Normal

CodePudding user response:

Here you have another option with case_when. With this option, you might add more conditions.

df %>% mutate(
  B = case_when(
    A >= 400 ~ 'nonNormal',
    A < 400 ~ 'Normal'
  )
)
  • Related