Sum the product by row on two data / matrix in r

I have two data frames, each of which has two columns. They can be matrices with the same dimensions, if this helps in the calculations.

What I want to do is the total product of these data frames of the corresponding positions / lines.

For example, the solution should be next in one column.

21 = 1*1+10*2 42 = 2*1 +20*2 63 = 3*1 + 20*2 a=data.frame(c_1=c(1,2,3),c_2=c(10,20,30)) b=data.frame(c2_1=c(1,1,1),c2_2=c(2,2,2)) 
+5
source share
2 answers

you can try something like

 rowSums(a*b) [1] 21 42 63 
+7
source

you can use

vector product

function in R. This is a fairly straightforward function.

0
source

All Articles