Reputation: 25
Hy there I have a table with some observations in different depths, where i d like to replace the Value of few observations with NA, if the Value of Observation 7 is negative, but otherwise it should keep its value. For example, if obs 7 in depth=2 is negative (e.g. -8), replace the values of observation 1,2,7 and 9 with NA. Otherwise do nothing
worksheet <- some_data%>%
do_some_stuff%
mutate_at(vars(obs7,obs1,obs2,obs9), if_else(obs7<=0,NA, ??)
Upvotes: 2
Views: 183
Reputation: 886938
Here, we need to only change the values in the column where 'ob7' is less than or equal to 0. In that case, the 'yes' part would be NA
and the 'no' would be the data column itself. In tidyverse, it would be represented as .
as there is a ~
library(tidyverse)
some_data %>%
mutate_at(vars(obs7, obs1, obs2, obs9), list(~ ifelse(ob7 <= 0, NA, .)))
NOTE: Here, we are using ifelse
instead of if_else
as if_else
is particular about type checking and from the question it is not clear about the type of columns of interest. If the columns are integer, then replace NA
with NA_integer_
and if it is double, NA_real_
within if_else
Upvotes: 4