0

Input data :

df1 <- data.frame(Category = c("a","b","c"),
                  Col1 = c(0.1,0.25,0.03),
                  Col2 = c(0.5,0.73,0.55),
                  Col3 = c(0.043,0.66,0.07))

df2 <- data.frame(Category = c("a","a","a","b","b","c","c","c","b"),
                  Col1 = c(1,2,3,4,5,6,7,8,9),
                  Col2 = c(9,8,7,6,5,4,3,2,1),
                  Col3 = c(5,6,7,8,9,4,3,2,1))

Both the data frames need to be multiplied to get the following result: enter image description here

In order to simply: enter image description here

Let's Code
  • 99
  • 7

1 Answers1

1

One way would be:

cbind(Category = df2$Category, df1[match(df2$Category, df1$Category),][-1] * df2[-1])

    Category Col1 Col2  Col3
1          a 0.10 4.50 0.215
1.1        a 0.20 4.00 0.258
1.2        a 0.30 3.50 0.301
2          b 1.00 4.38 5.280
2.1        b 1.25 3.65 5.940
3          c 0.18 2.20 0.280
3.1        c 0.21 1.65 0.210
3.2        c 0.24 1.10 0.140
2.2        b 2.25 0.73 0.660
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56