R programming error sum of products

+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 in Data Analytics by Tyrion anex
• 8,700 points
764 views

1 answer to this question.

0 votes

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))
answered Mar 6, 2019 by Sophie may
• 10,620 points

Related Questions In Data Analytics

+2 votes
1 answer

“subscript out of bounds” Error in r programming

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

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
9,571 views
+3 votes
2 answers

Error: could not find function - R Programming

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

answered Aug 23, 2019 in Data Analytics by anonymous
• 33,030 points
33,710 views
0 votes
1 answer

"no applicable method" Error in r programming

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

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
2,534 views
+1 vote
1 answer

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

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

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
2,368 views
+1 vote
1 answer

R programming error

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

answered Dec 18, 2018 in Data Analytics by Tyrion anex
• 8,700 points
664 views
+1 vote
1 answer

R Programming: Market Basket Analysis Error

The basket.sorted() has less than 5 rules. Refer ...READ MORE

answered Feb 12, 2019 in Data Analytics by Sophie may
• 10,620 points
1,515 views
+1 vote
1 answer

R Programming: regexpr error

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

answered Feb 21, 2019 in Data Analytics by Tyrion anex
• 8,700 points
630 views
+1 vote
1 answer

R programming: Graphs

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

answered Feb 21, 2019 in Data Analytics by Tyrion anex
• 8,700 points
607 views
0 votes
1 answer

R programming: Unexpected symbol error

Format your code this way: myfunction <- function() ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 10,620 points
3,256 views
0 votes
1 answer

R Programming error in 'fert'

You're using a factor: fert <- factor(c(50,20,10,10,20,50)) levels(fert) #[1] ...READ MORE

answered Dec 28, 2018 in Data Analytics by Sophie may
• 10,620 points
727 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP