Reputation: 37
i have the following issue:
In my data frame (89 columns) I have 4 of them which have the values in a negative way as you can see in the following image
![1]: https://i.sstatic.net/ZFF0U.png
So I would like to know how I could mutate that specific columns of my data frame in order to make the values of them positive (absolute value).
Many thanks
Upvotes: 0
Views: 1454
Reputation: 41220
A general solution:
library(dplyr)
data %>% mutate_if(function(x) all(x<0), function(x) abs(x))
Upvotes: 0
Reputation: 145785
Here's one option:
library(dplyr)
your_data %>%
mutate(across(c("DAYS_BIRTH", "DAYS_EMPLOYED", "DAYS_REGISTRATION", "DAYS_ID_PUBLISH"), abs))
Depending on which columns you want to mutate and which you want to leave, you might be able to use a simpler select helper, like mutate(across(starts_with("DAYS"), abs))
, for example.
Upvotes: 2