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:
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)
)
)