I would like to replace values by NA in specific rows if a specific character is found within the current value, f.e. if a value contains "<" (lower than), f.e. "<7.5" I would like to replace the whole value by NA.
Examples:
Column A: 3, 4, 8, <5.6, 1, 3
Column B: 7, 4, <6, 1, <2.2, 8
should be converted to:
Column A: 3, 4, 8, NA, 1, 3
Column B: 7, 4, NA, 1, NA, 8
I found this example here (https://dplyr.tidyverse.org/reference/na_if.html) with mutate and na_if(), but it requires to match the whole string, f.e.
y <- c("abc", "def", "", "ghi")
na_if(y, "def")
So "def" would be replaced by NA. But if I use
y <- c("abc", "def", "", "ghi")
na_if(y, "ef")
nothing is replaced. There is also an example with
library(dplyr)
data <- starwars
data %>%
select(name, eye_color) %>%
mutate(name = na_if(name, "Luke Skywalker")) %>%
mutate(eye_color = na_if(eye_color, "unknown")) -> dataedited
And this code works perfect for me, but also need exact match instead of just a part of the string. This way I could edit each column manually, maybe there is a way to perform this across multiple columns. I would like to convert values to NA if name contains "sky", or eye contains "unkn".
Can anyone help me?
Thank you!