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

Time:03-11

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.

CodePudding user response:

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

CodePudding user response:

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