I have a dataframe, and I want to assign NA to non-positive values of specific columns.
I’ll try to realize a minimal reproducible example with the mtcars dataframe where I’ll try to change with NA the values of the two columns cyl and disp that are <= 5.
library(dplyr)
view(mtcars)
Nomi <- c("cyl", "disp")
for(i in Nomi) {
mtcars$i[mtcars$i <= 5] <- NA
}
Nomi is the vector with the names of mtcars columns where I want to change values with NA.
I don’t fully understand the error message I get, I’d like to find a solution that can make the changes in NA.
>Solution :
If you are comfortable with dplyr, you can use an ifelse statement in mutate(across()). It means across cyl and disp column, if the values in these two columns are <= 5, replace it with NA. Otherwise, keep the original value (.x).
Note that this will only output results to the console WITHOUT actually replacing the mtcars dataset.
library(dplyr)
mtcars %>% mutate(across(c(cyl, disp), ~ ifelse(.x <= 5, NA, .x)))