0

I trying to convert all negative values to 0 in specific columns of a dataframe. How would I for an example convert negative values to zero in column 3:5 and 8 in the mtcars data?

mtcars <- mtcars%>%
mutate(across(c(3:5,8), funs(replace(., .<0, 0)))

Thanks a lot for any help!

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
Tiptop
  • 533
  • 5
  • 19

1 Answers1

1

This could be achieved like so:

library(dplyr)

mtcars <- mtcars %>%
  mutate(across(c(3:5,8), ~ if_else(. < 0, 0, .)))
stefan
  • 90,330
  • 6
  • 25
  • 51