create new columns with mutate_all

The usage of funs would be deprecated in favor of list from dplyr_0.8.0 So, the option would be

library(dplyr)
df %>%
    mutate_at(vars(Revenue:Rent), list(percentage_of_rent = ~  ./Rent))
#  Year Revenue Cost Rent Revenue_percentage_of_rent Cost_percentage_of_rent Rent_percentage_of_rent
#1 2016    3000    4  100                         30                    0.04                       1
#2 2017    4000    5  100                         40                    0.05                       1
#3 2018    5000    6  100                         50                    0.06                       1

Name the column in the function in mutate_at

library(dplyr)
df %>% mutate_at(vars(Revenue:Rent), funs(percentage_of_rent = . /Rent))

You can do it with mutate_all but then it will also divide the Year column by Rent which I suppose you don't need.

A workaround to use mutate_all would be

df %>% select(-Year) %>% mutate_all(funs(percentage_of_rent = . /Rent))

but you loose Year column here.

Tags:

R

Dplyr

Mutate