Trying to add a new binary column to a dataset that will return a "1" if it meets certain requirements (below) and a "0" if not.
(x == "1") & (width < 1 | width > 5 | height < 1.5 | height > 3.5)
Any idea how to do this??
Trying to add a new binary column to a dataset that will return a "1" if it meets certain requirements (below) and a "0" if not.
(x == "1") & (width < 1 | width > 5 | height < 1.5 | height > 3.5)
Any idea how to do this??
a reproducible example is the best way to get an exact answer for your needs. but I would do something like..
library(dplyr)
df2 <- df %>%
mutate(new_column = ifelse(width > 5, 1, 0))
ifelse
works like ifelse(expression, value_if_true, value_if_false)
and the expression must resolve to TRUE
or FALSE
.
This should also work:
df2 <- df %>%
mutate(new_column = ifelse(((x == "1") & (width < 1 | width > 5 | height < 1.5 | height > 3.5)), 1, 0)