[R] Multiplying each row of data.frame by a row in another data.frame

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Sat Apr 9 19:37:32 CEST 2022


As usual, it would be really nice if we KNEW what the parameters of a problem are before offering many solutions based on guesses.
The trivial solution seems to be component-wise multiplication of a vector in sequential steps against each column of a data.frame.
Unless you have tons of columns, this is easy and straightforward.
If the goal is to do it in a more general way so that no matter how manycolumns there are, then it depends on whether this is homework and what is valid to use. And, in that case, people may not wish to provide a solution directly.
There are MANY possible solutions including some that have been offered whichtake the vector you are multiplying by and expand it into  a matrix made of copies.
My text may wrap funny no matter what I do so here are two data structures:
orig <- data.frame(a=1:3, b=3:1, c=c(10,15,20))

vec <- c(1, 10, 100)



The first has columns going from 1 to 3 and from 3 to 1 and then by fives from 10 to 20.
The second is simply the powers of 10 from 1 to 100.
If you multiply the data.frame by the vector in R, it multiplies various ways you can change by using a transpose but not necesarily the results intended.

Many would suggest some of the newer packages, especially dplyr in the tidyverse.
The following works for me. It takes the original column and mutates every column back into the same name by multiplying the "current" column denoted by ".x" against the vector.
mutate_all(orig, ~ .x * vec)


Again, as my text using this mailer gets shown oddly to someone no matter what I do, I am not sowing the results or trying to show multi-line code. 
Do note the dplyr packages themselves keep mutating and things get deprecated so you may find that it suggests another way to do the above and especially if you search the internet and find advice from various versions. Be that as it may, there are various solutions.



-----Original Message-----
From: Kelly Thompson <kt1572757 using gmail.com>
Cc: r-help using r-project.org <r-help using r-project.org>
Sent: Sat, Apr 9, 2022 12:49 pm
Subject: Re: [R] Multiplying each row of data.frame by a row in another data.frame

Does this produce the desired results?
Also, even if this produces your desired results, I am not sure it is
the "best" way to do this. There might be better ways.

data.frame( t ( t(val_df) * (weights$value) )

On Fri, Apr 8, 2022 at 9:57 PM maithili_shiva--- via R-help
<r-help using r-project.org> wrote:
>
> Dear R forum
> weights <- data.frame(id = c("ABC", "DEF",  "ONS"), value = c(1, 2, 5))
> val_df <- data.frame(ABC = c(10, 20, 10, 10, 10), DEF = c(1, 2, 10, 2, 5), ONS = c(100, 100, 200, 100, 100))
>
> > weights
>    id          value1 ABC      12 DEF      23 ONS      5
>
> > val_df
>  ABC DEF ONS1  10  1    1002  20  2    1003  10  10    2004  10  2    1005  10  5    100
> I wish to multilpy each row of data.frame val_df by value column of data.frame weights.
>  ABC    DEF    ONS1  10*1  1*2  100*52  20*1  2*2  100*53  10*1  10*2  200*54  10*1  2*2  100*55  10*1  5*2  100*5
> ie I wish to have output as
> OUTPUT
>  ABC    DEF    ONS1  10    2          5002  20    4        5003  10    20      10004  10    4        5005  10    10    500
>
> O have tried sweep function, even matrix multiplcation etc, but nothing seems to be working.
> Please guide
> Regards
> Maithili
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list