考虑以下数据框:
df <- data.frame(row_id = c("r1","r2","r3","r4","r1","r2","r3","r4"),
v1 = c(3,2,5,2,5,2,6,4),
v2 = c(4,3,5,3,7,4,6,7))
我想用“row_id”取和积。也就是说,对于具有 row_id:"r1" 的行,我想做以下计算:(3*4)+(5*7)。等等。
因此,我最终将得到以下矩阵:
df1 <- data.frame(row_id = c("r1","r2","r3","r4"),
v1 = c(47,14,61,34))
任何帮助将不胜感激。
谢谢。