如何计算purrr
中的点积?作为一个代表,这是一个简单的示例。
#fake data
X <- as_tibble(list(a = rnorm(10,0,1),
b = rnorm(10,10,1),
c = rnorm(10,100,1)))
z <- c(1,0,1)
#make tibble matrix
X_matrix <- X %>% as.matrix()
X_matrix
a b c
[1,] 0.01182775 9.032966 100.95322
[2,] 0.85718250 10.015310 102.30181
[3,] -0.06742915 10.535482 100.21764
[4,] -0.18236798 9.052234 99.37345
[5,] -0.32151084 10.329401 98.81186
[6,] 2.94303948 9.994800 99.93874
[7,] 0.03299169 9.079023 99.73501
[8,] 0.06518171 8.841637 99.91130
[9,] -0.71944580 10.281631 100.32533
[10,] 1.49983359 10.776108 99.35903
点积为sum(a*z[1] + b*z[2] + c*z[3])
X_matrix %*% z
[,1]
[1,] 100.96505
[2,] 103.15900
[3,] 100.15021
[4,] 99.19108
[5,] 98.49035
[6,] 102.88178
[7,] 99.76800
[8,] 99.97648
[9,] 99.60588
[10,] 100.85886
理想情况下,我想将点积作为列添加到X