'How to replace non-positive values of some columns of my dataframe with NA in r

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 1:[1]

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)))

Solution 2:[2]

In base R, you can directly do the replacement on the dataframe.

Nomi <- c("cyl", "disp")
df <- mtcars
df[Nomi][df[Nomi] <= 5] <- NA
df

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 benson23
Solution 2 Ronak Shah