in Education by
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.) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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

Related questions

0 votes
    I looked for solutions here: Multiply columns in a data frame by a vector and here: What is the right way to ... data frame. Taking teh data example from the first link: c1...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I looked for solutions here: Multiply columns in a data frame by a vector and here: What is the right way to ... data frame. Taking teh data example from the first link: c1...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Using the ________ vector, we create a species factor and bind it to the columns of iris.df. (a) snames (b ... and Out of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    How to sort a data.table using vector of multiple columns in R? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    Columns can be arranged in descending order too by using the special ____ operator. (a) asc() (b) desc( ... Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I want to bind two data frames with different set of columns and I also want to retain all the columns that fails to ... do that in R? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to remove some questions from data frame, I know how to delete them individually using df$x...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Calculate the mole fraction of benzene in solution containing 30% by mass in carbon tetrachloride. Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    Calculate the osmotic pressure in pascals exerted by a solution prepared by dissolving 1.0g of polymer of molar mass 185 ... at 37∘C . Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    Concentrated nitric acid used in the laboratory work is 68% nitric acid by mass in aqueous solution. What should be ... 1.504gmL-1 ? Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    Calculate the percentage composition in terms of mass of solution obtained by mixing 300g of a 25% and 400g of a 40% solution by mass. Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    A 5% solution (by mass) of cane sugar in water has freezing point of 271 K. Calculate the freezing point of a 5% ... water is 273.15 K. Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    An aqueous solution of K2SO4 is diluted by adding water. How the values of G,k,∧m and ∧eq vary ? Select the correct answer from above options...
asked Jan 4, 2022 in Education by JackTerrance
0 votes
    Exam total: 40 mar Marks: 1 We can remove a keyframe by right-clicking on the frame and selecting the ... T Formula Camera Gallery Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
...