Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tidyverse solution for multiplying columns by a vector

I looked for solutions here: Multiply columns in a data frame by a vector and here: What is the right way to multiply data frame by vector?, but it doesn't really work.

What I want to do is a more or less clean tidyverse way where I multiply columns by a vector and then add these as new columns to the existing data frame. Taking teh data example from the first link:

c1 <- c(1,2,3)
c2 <- c(4,5,6)
c3 <- c(7,8,9)
d1 <- data.frame(c1,c2,c3)

  c1 c2 c3
1  1  4  7
2  2  5  8
3  3  6  9

v1 <- c(1,2,3)

my desired result would be:

  c1 c2 c3 pro_c1 pro_c2 pro_c3
1  1  4  7      1      8     21
2  2  5  8      2     10     24
3  3  6  9      3     12     27

I tried:

library(tidyverse)
d1 |>
  mutate(pro = sweep(across(everything()), 2, v1, "*"))

But here the problem is the new columns are actually a data frame within my data frame. And I'm struggling with turning this data frame-in-data frame into regular columns. I assume, I could probably first setNames on this inner data frame and then unnest, but wondering if there's a more direct way by looping over each column with across and feed it with the first/second/third element of v1?

(I know I could probably also first create a standalone data frame with the three new multiplied columns, give them a unique name and then bind_cols on both, d1 and the df with the products.)

like image 752
deschen Avatar asked Sep 17 '25 15:09

deschen


1 Answers

This is perhaps ridiculous, but you could use

library(dplyr)

d1 %>% 
  mutate(across(everything(), 
                ~.x * v1[which(names(d1) == cur_column())],
                .names = "pro_{.col}"))

which returns

  c1 c2 c3 pro_c1 pro_c2 pro_c3
1  1  4  7      1      8     21
2  2  5  8      2     10     24
3  3  6  9      3     12     27
like image 67
Martin Gal Avatar answered Sep 19 '25 06:09

Martin Gal