Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading