Percentage using across

Hello everyone,

I would like to create multiple columns with a function that divides each row value with the rowSum using across

the table has three numeric columns x,y, and z and another column called totals which is the rowSums(x:z)

table %>% mutate(across(.cols = c(x,y,z), 
    .fns = .col/.$totals, .names = "{.col}_perc"))

any help possible.

PS. its my first post here so forgive anything not clear in the question. i hope to learn more as I interact here

It seems you want the following

library(tidyverse)

(mytable <- data.frame(x=1:2,
                      y=2:3,
                      z=3:4))

mytable %>% mutate(across(.cols = c(x,y,z), 
                        .fns = \(x_)x_/rowSums(pick(x:z)),
                        .names = "{.col}_perc"))
1 Like

WOW!!!!.... thanks so much for the quick reply .... and it works! :smile:
. Be blessed!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.