首页 > 解决方案 > data.frame 中所有变量对的元素乘法之和

问题描述

除了简单地为下面的 data.framecrossprod执行(.1*.3)+(.2*.4)和输出之外,它们是否还有任何 BASE 功能?.11

注意:这是一个玩具示例,data.frame 可以有任意数量的列。

x = data.frame(a = c(.1, .2), b = c(.3, .4))

# Desired Output
(.1*.3)+(.2*.4) #= .11

crossprod(as.matrix(x))

# Current output
     a    b
a 0.05 0.11
b 0.11 0.25

标签: rdataframematrixvectoralgebra

解决方案


c(do.call("%*%", x))

[1] 0.11

甚至

x$a%*%x$b

推荐阅读