0

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
PythonNoob
  • 65
  • 3
  • 1
    Use `ifelse` i.e. `df$B <- with(df, ifelse(A >=400, "nonNormal", "Normal"))` – akrun Jun 24 '22 at 16:17
  • Does this answer your question? [Create categories by comparing a numeric column with a fixed value](https://stackoverflow.com/questions/15016723/create-categories-by-comparing-a-numeric-column-with-a-fixed-value) – user438383 Jun 24 '22 at 16:27

1 Answers1

0

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'
  )
)
Isaac
  • 382
  • 2
  • 7