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

Making all categorical variables start at 0 in R

I have a data frame like so where every column is a categorical encoding:

> race <- factor(c(0,1,0,1,1))
> income <- factor(c(1,1,1,0,0))
> sex <- factor(c(1,1,1,3,2))
> df <- data.frame(race, income, sex)
> df
  race income  sex
1    0      1    1
2    1      1    1
3    0      1    1
4    1      0    3
5    1      0    2

How can I dynamically program in R dplyr so that every column starts at 0.
For example race and income wouldn’t be changed because the lowest value is already 0.
But Sex would be need to changed so that every number is subtracted by 1.

expected output:

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

  race income  sex
1    0      1    0
2    1      1    0
3    0      1    0
4    1      0    2
5    1      0    1

Ideally the solution would use mutate and across, but I can’t seem to get a solution.

>Solution :

df %>%
  mutate(
    across(everything(), as.numeric),
    across(everything(), ~.-min(.)),
    across(everything(), as.factor)
  )
)

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