-1

Given a dataframe

category <- c("A", "B", "C")
r1 <- c(2, 2, 2)
r2 <- c(3, 3, 3)
r3 <- c(1, 1, 1)
r4 <- c(4, 4, 4)
df <- data.frame(category, r1, r2, r3, r4)

How do I calculate and add a new row D by multiplying values in A and B per column and dividing by the sum of C?

jmutua
  • 290
  • 1
  • 12
  • Are you sure you want to add a new row? Or do you mean a new column? Because values that belong together should be in the same row not in the same column. – tamara d Jul 31 '20 at 08:01

1 Answers1

0

Perhaps we can do

 df$r5 <- (df$r2 * df$r4)/sum(df$r4)
akrun
  • 874,273
  • 37
  • 540
  • 662