+1 vote

I'm trying to find the sum of products of two different data frames.

```data<-a1 a2 a3 a4
4   6  8  5```

Here, a1 a2 a3 a4 are column names

The other data frame is:

```data2<-p1 p2 p3 p4
3  4  5  6
5  6  8  4
4  6  6  8
3  5  8  9```

my result should be like this:

```result <- a1*P1+a2*p2+a3*p3*a4*p4

result1 <- 4*3+6*4+8*5+5*6

result2 <- 4*5+6*6+8*8+5*4   ```

and so on...

How can this be done?

Mar 6, 2019 645 views

## 1 answer to this question.

Best soln is to integrate R linear algebra:

```> as.matrix(data2) %*% unlist(data)
#     [,1]
#[1,]  106
#[2,]  140
#[3,]  140
#[4,]  151```

or you can sweep:

```> rowSums(sweep(as.matrix(data2), 2, unlist(data), `*`))
#[1] 106 140 140 151```

Where data is:

```data=data.frame(a=4,b=6,c=8,d=5)
data2=data.frame(a=c(3,5,4,3),b=c(4,6,6,5),c=c(5,8,6,8),d=c(6,4,8,9))```
• 10,610 points

## “subscript out of bounds” Error in r programming

This error is likely to occur when ...READ MORE

## Error: could not find function - R Programming

Yes, Just like @Maverik said, It happens ...READ MORE

## "no applicable method" Error in r programming

This is caused by using an object-oriented ...READ MORE

+1 vote

## "subscript out of bounds" error in while executing simple R program

This is caused by trying to access ...READ MORE

+1 vote

## R programming error

Alright, you can either use gsub to match the ...READ MORE

+1 vote

+1 vote

## R Programming: regexpr error

The below code will help: gregexpr("D", x) # [[1]] # ...READ MORE

+1 vote

## R programming: Graphs

Try this: ggplot(dfc, aes(x=x, y=y, colour=f, fill=f, ymin=y-se, ...READ MORE