在 R 中,两个向量的乘积求和运算等价于 Excel 中的 SUMPRODUCT 函数是什么?


Excel 中的 SUMPRODUCT 函数将两个或多个数组的每个值与其对应值相乘,然后将结果相加。例如,如果我们在 Excel 中的 A1、A2 中有 1、2,在 B1 和 B2 中有 2、2,则 SUMPRODUCT 将 1*2 和 2*2 相乘,然后对这两个乘积求和。在 R 中,我们使用 crossprod 函数来实现相同的功能。

示例

 实时演示

x1<-rnorm(50,1.5)
x1

输出

[1] 1.99359076  1.88748588 0.72095131 3.10185637 -0.90790685 0.51694935
[7] -0.05495242 1.49991243 2.54843446 2.79770387 -0.59328378 1.44738196
[13] 2.31391231 0.92844811 2.45329835 0.70609159 2.23518856  0.61631980
[19] 2.75137005 1.85486279 2.05531035 2.20960910 0.20037395  2.68390848
[25] 1.40389120 0.17282489 0.36633216 2.18996730 -0.10523904 -0.11863699
[31] 1.39480555 3.04977324 4.51243878 2.50746627 2.03817155  3.08036664
[37] 0.39284354 2.59858026 2.77797656 0.94338344 1.03293175  1.46812123
[43] 2.40907064 1.61772641 6.14482972 1.10566435 0.57390910  0.87722282
[49] 1.62433138 1.80906998

示例

 实时演示

y1<-rnorm(50,2.5)
y1

输出

[1] 1.8783136 2.7331609 4.7371202 3.9985253 2.8102108 3.7087233 1.8897202
[8] 2.3324918 2.3859289 3.6085963 1.2847904 2.9825826 1.8944861 2.8596306
[15] 4.1415533 2.7008292 1.5199610 1.5111808 2.2279718 0.2254175 3.3921581
[22] 2.9906191 1.1403147 2.6683893 3.7104668 2.4567271 4.1465060 2.4379809
[29] 3.1013512 2.5057454 1.3899342 4.0594080 2.9544847 2.9267532 1.5415416
[36] 2.8127391 1.6027450 2.1677937 2.7763609 2.2713097 3.1494273 4.1289289
[43] 2.8311815 2.6957011 2.3890127 1.3183123 1.9293547 3.3658244 2.2184533
[50] 2.1856964

示例

crossprod(x1,y1)

输出

[,1]
[1,] 221.1997

示例

 实时演示

x2<-rpois(100,2)
x2

输出

[1] 2 0 1 2 1 0 2 0 1 1 2 2 3 2 3 1 2 4 4 1 0 3 2 2 0 2 0 2 1 1 4 4 3 2 0 0 1
[38] 3 2 0 1 2 2 1 6 2 3 3 3 0 3 5 1 2 4 0 2 0 0 3 2 1 1 2 2 3 1 5 2 3 3 1 2 4
[75] 3 2 3 2 4 2 1 1 4 3 1 3 3 1 1 2 1 3 0 3 4 1 3 1 2 3

示例

 实时演示

y2<-rpois(100,5)
y2

输出

[1] 5 6 3 4 5 3 8 7 6 4 6 7 5 7 6 10 6 6 4 2 1 1 3 4 4
[26] 7 4 6 8 6 5 4 6 7 5 4 7 6 6 3 5 5 7 6 6 5 4 5 3 2
[51] 3 4 10 5 6 3 7 5 6 4 5 3 7 2 8 3 6 2 4 3 4 9 2 6 4
[76] 3 1 6 5 4 5 5 5 3 4 7 6 4 4 5 4 2 8 7 3 5 4 6 4 7

示例

crossprod(x2,y2)

输出

[,1]
[1,] 954

示例

 实时演示

x3<-runif(50,2,5)
x3

输出

[1] 4.066125 4.753823 4.404160 4.986185 4.123433 4.390743 2.763934 2.751122
[9] 2.188381 4.402746 3.286630 4.485267 3.004045 4.475638 4.382088 4.394354
[17] 2.014214 3.078435 2.781432 2.611495 2.672326 4.700600 4.553896 4.691448
[25] 4.441690 4.278304 4.523280 2.590837 4.312463 4.180315 4.868710 2.495915
[33] 4.608362 2.661993 4.313882 2.302652 4.491288 2.926825 2.148216 4.759026
[41] 2.396023 3.255598 4.739009 3.542550 2.817052 2.977642 3.878959 4.867458
[49] 2.231442 4.950959

示例

 实时演示

y3<-runif(50,2,10)
y3

输出

[1] 7.845753 4.834397 4.995095 6.552002 4.471001 9.203054 6.124359 6.622755
[9] 3.877650 7.280489 7.774599 2.747195 9.551918 9.056671 6.434740 7.348743
[17] 4.792461 9.166887 8.958969 7.335471 8.918295 5.435485 2.191420 2.603769
[25] 6.093804 8.087972 8.973729 4.278366 9.924628 3.864139 9.443934 3.410300
[33] 7.659189 5.001502 4.241095 9.632373 8.544051 7.156040 4.299191 8.890373
[41] 7.686046 8.845440 7.641526 3.969289 3.926754 3.330756 9.740170 4.345852
[49] 8.746772 5.178304

示例

crossprod(x3,y3)

输出

[,1]
[1,] 1214.499

示例

 实时演示

x4<-sample(1:10,100,replace=TRUE)
x4

输出

[1] 2 4 10 7 6 6 1 7 7 5 10 1 7 2 3 4 4 3 4 10 6 8 5 1 8
[26] 9 1 8 3 1 7 6 5 8 8 8 2 7 6 6 9 3 8 2 9 7 9 10 6 7
[51] 1 10 1 5 6 1 5 8 9 1 1 5 1 10 1 2 7 1 8 3 5 7 1 10 1
[76] 2 9 10 4 9 3 7 10 8 7 6 4 10 5 2 5 6 5 9 2 3 6 5 1 5

示例

 实时演示

y4<-sample(1:50,100,replace=TRUE)
y4

输出

[1] 17 37 38 31 6 13 24 38 46 35 14 44 37 40 12 15 10 31 29 33 9 47 49 40 34
[26] 40 9 1 21 30 43 27 31 16 29 2 21 9 45 42 44 6 14 42 9 12 10 42 39 11
[51] 50 31 2 12 47 3 41 19 45 21 42 20 26 13 24 41 38 6 30 22 45 44 22 17 32
[76] 50 43 3 47 34 28 18 12 11 3 33 27 26 35 5 17 36 22 44 33 19 24 32 44 13

示例

crossprod(x4,y4)

输出

[,1]
[1,] 14325

示例

 实时演示

x5<-runif(50,-5,-2)
x5

输出

[1] -2.864014 -3.920292 -2.697349 -3.547204 -3.203409 -4.408416 -4.046039
[8] -2.307824 -2.026316 -2.471796 -2.572194 -3.986755 -4.363628 -3.635759
[15] -2.082331 -4.251178 -4.681768 -3.034604 -3.418636 -3.981439 -2.494370
[22] -4.450702 -3.123108 -2.693205 -4.557498 -3.379469 -2.610095 -4.637020
[29] -3.876702 -4.587460 -3.603890 -2.903007 -4.404766 -2.155481 -3.569201
[36] -2.292755 -3.353520 -3.583467 -4.595250 -2.439884 -3.363896 -2.014880
[43] -4.517038 -4.252916 -3.634848 -2.152265 -3.678823 -2.939615 -2.467037
[50] -4.190786

示例

 实时演示

y5<-runif(50,-3,-2)
y5

输出

[1] -2.915089 -2.658054 -2.713909 -2.852047 -2.730487 -2.856746 -2.276689
[8] -2.369880 -2.197784 -2.566198 -2.856807 -2.027856 -2.516569 -2.975913
[15] -2.684511 -2.309696 -2.635481 -2.306488 -2.903054 -2.646775 -2.825036
[22] -2.049512 -2.873639 -2.753928 -2.289014 -2.981482 -2.144614 -2.532343
[29] -2.639120 -2.292755 -2.441756 -2.450644 -2.729074 -2.352912 -2.022934
[36] -2.920817 -2.479174 -2.324256 -2.309613 -2.697988 -2.990217 -2.537785
[43] -2.087993 -2.075586 -2.267299 -2.999600 -2.562218 -2.137688 -2.881335
[50] -2.986426

示例

crossprod(x5,y5)

输出

[,1]
[1,] 430.8493

更新于: 2020-09-08

2K+ 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告