通常,我们需要找到R向量中某个值的幂或所有值的幂,尤其是在处理多项式模型的情况下。可以像在Excel中一样通过使用^符号来完成此操作。例如,如果我们有一个向量x,则x中所有值的平方可以发现为x ^ 2。
x1<-1:10 x1
输出结果
[1] 1 2 3 4 5 6 7 8 9 10
square_x1<-x1^2 square_x1
输出结果
[1] 1 4 9 16 25 36 49 64 81 100
x2<-rpois(50,2) x2
输出结果
[1] 5 3 4 2 1 8 1 4 0 2 3 5 2 3 4 6 3 3 4 2 4 3 2 1 1 0 1 0 2 1 1 0 1 1 4 2 3 4 [39] 1 2 2 2 1 1 6 0 4 1 3 2
square_x2
输出结果
[1] 25 9 16 4 1 64 1 16 0 4 9 25 4 9 16 36 9 9 16 4 16 9 4 1 1 [26] 0 1 0 4 1 1 0 1 1 16 4 9 16 1 4 4 4 1 1 36 0 16 1 9 4
x3<-rpois(100,10) x3
输出结果
[1] 9 10 12 15 12 7 10 11 9 9 13 7 8 11 7 12 11 12 7 10 9 4 12 11 8 [26] 9 10 16 12 9 11 8 9 8 8 10 7 11 7 11 16 9 10 10 11 14 11 12 7 9 [51] 4 14 4 13 11 13 9 9 13 6 15 11 11 7 10 19 12 6 11 14 11 7 7 16 8 [76] 9 9 5 15 12 7 9 10 10 9 14 3 9 12 12 13 9 11 9 9 15 10 15 5 8
x3_power_3<-x3^3 x3_power_3
输出结果
[1] 729 1000 1728 3375 1728 343 1000 1331 729 729 2197 343 512 1331 343 [16] 1728 1331 1728 343 1000 729 64 1728 1331 512 729 1000 4096 1728 729 [31] 1331 512 729 512 512 1000 343 1331 343 1331 4096 729 1000 1000 1331 [46] 2744 1331 1728 343 729 64 2744 64 2197 1331 2197 729 729 2197 216 [61] 3375 1331 1331 343 1000 6859 1728 216 1331 2744 1331 343 343 4096 512 [76] 729 729 125 3375 1728 343 729 1000 1000 729 2744 27 729 1728 1728 [91] 2197 729 1331 729 729 3375 1000 3375 125 512
x4<-rpois(100,10) x4
输出结果
[1] 13 10 11 8 12 12 7 11 6 11 8 13 9 11 8 3 4 7 11 14 7 9 8 12 8 [26] 6 11 8 12 6 9 5 11 9 10 12 9 5 6 10 9 13 13 8 14 6 9 14 12 10 [51] 8 9 9 4 11 11 11 8 12 9 9 6 9 7 9 6 5 13 8 10 8 4 10 10 7 [76] 16 8 13 11 8 6 13 12 8 7 11 9 8 14 8 5 9 12 7 6 9 11 14 14 8
x4_power_1_by_4<-x4^(1/4) x4_power_1_by_4
输出结果
[1] 1.898829 1.778279 1.821160 1.681793 1.861210 1.861210 1.626577 1.821160 [9] 1.565085 1.821160 1.681793 1.898829 1.732051 1.821160 1.681793 1.316074 [17] 1.414214 1.626577 1.821160 1.934336 1.626577 1.732051 1.681793 1.861210 [25] 1.681793 1.565085 1.821160 1.681793 1.861210 1.565085 1.732051 1.495349 [33] 1.821160 1.732051 1.778279 1.861210 1.732051 1.495349 1.565085 1.778279 [41] 1.732051 1.898829 1.898829 1.681793 1.934336 1.565085 1.732051 1.934336 [49] 1.861210 1.778279 1.681793 1.732051 1.732051 1.414214 1.821160 1.821160 [57] 1.821160 1.681793 1.861210 1.732051 1.732051 1.565085 1.732051 1.626577 [65] 1.732051 1.565085 1.495349 1.898829 1.681793 1.778279 1.681793 1.414214 [73] 1.778279 1.778279 1.626577 2.000000 1.681793 1.898829 1.821160 1.681793 [81] 1.565085 1.898829 1.861210 1.681793 1.626577 1.821160 1.732051 1.681793 [89] 1.934336 1.681793 1.495349 1.732051 1.861210 1.626577 1.565085 1.732051 [97] 1.821160 1.934336 1.934336 1.681793
x5<-rpois(100,10) x5
输出结果
[1] 12 9 16 11 10 6 12 16 15 14 7 5 6 14 6 8 10 11 14 10 10 6 9 11 13 [26] 12 11 20 7 7 5 18 11 13 7 15 15 6 9 10 13 6 6 7 8 8 16 10 7 9 [51] 12 4 8 9 7 9 10 14 13 21 7 14 5 9 9 13 7 6 12 11 10 12 6 14 8 [76] 11 7 9 8 10 9 12 15 8 6 7 8 4 12 10 9 11 11 11 15 7 10 12 16 12
x5_power_3_by_4<-x5^(3/4) x5_power_3_by_4
输出结果
[1] 6.447420 5.196152 8.000000 6.040105 5.623413 3.833659 6.447420 8.000000 [9] 7.621991 7.237624 4.303517 3.343702 3.833659 7.237624 3.833659 4.756828 [17] 5.623413 6.040105 7.237624 5.623413 5.623413 3.833659 5.196152 6.040105 [25] 6.846325 6.447420 6.040105 9.457416 4.303517 4.303517 3.343702 8.738852 [33] 6.040105 6.846325 4.303517 7.621991 7.621991 3.833659 5.196152 5.623413 [41] 6.846325 3.833659 3.833659 4.303517 4.756828 4.756828 8.000000 5.623413 [49] 4.303517 5.196152 6.447420 2.828427 4.756828 5.196152 4.303517 5.196152 [57] 5.623413 7.237624 6.846325 9.809898 4.303517 7.237624 3.343702 5.196152 [65] 5.196152 6.846325 4.303517 3.833659 6.447420 6.040105 5.623413 6.447420 [73] 3.833659 7.237624 4.756828 6.040105 4.303517 5.196152 4.756828 5.623413 [81] 5.196152 6.447420 7.621991 4.756828 3.833659 4.303517 4.756828 2.828427 [89] 6.447420 5.623413 5.196152 6.040105 6.040105 6.040105 7.621991 4.303517 [97] 5.623413 6.447420 8.000000 6.447420
x6<-sample(1:10,100,replace=TRUE) x6
输出结果
[1] 5 5 9 8 2 9 5 4 7 10 6 5 1 2 7 3 6 1 3 4 3 10 4 3 6 [26] 8 8 2 8 4 1 8 4 8 2 3 4 1 8 10 8 8 6 3 7 2 9 9 6 10 [51] 7 8 1 9 5 2 5 2 8 3 1 6 2 3 5 1 2 4 5 1 5 4 6 9 7 [76] 2 4 3 2 7 6 5 1 9 10 6 7 1 2 5 10 5 5 6 4 6 1 4 8 9
x6_2<-x6^2 x6_2
输出结果
[1] 25 25 81 64 4 81 25 16 49 100 36 25 1 4 49 9 36 1 [19] 9 16 9 100 16 9 36 64 64 4 64 16 1 64 16 64 4 9 [37] 16 1 64 100 64 64 36 9 49 4 81 81 36 100 49 64 1 81 [55] 25 4 25 4 64 9 1 36 4 9 25 1 4 16 25 1 25 16 [73] 36 81 49 4 16 9 4 49 36 25 1 81 100 36 49 1 4 25 [91] 100 25 25 36 16 36 1 16 64 81
x7<-rnorm(50,5,10) x7
输出结果
[1] 3.2380130 -5.5553674 17.3906895 16.2782636 -1.8551276 22.5405542 [7] 7.8591889 4.0376126 10.3603525 -2.0983122 7.7640085 8.0753874 [13] -3.2625302 9.4534281 -0.4951846 -9.4298196 2.7521265 8.0154121 [19] 6.7455055 -0.8284140 2.7099639 14.9538382 12.0254986 -5.6911766 [25] 3.8437799 11.9242421 -11.7194797 6.6024536 -2.7468949 6.1553066 [31] -5.3897772 6.7756489 21.8886362 16.7859536 1.1068733 -5.7264846 [37] 1.0465364 -3.4498760 -1.5594048 -20.0699144 6.6783996 17.1561048 [43] 7.5514595 21.1906289 4.9587869 8.1482000 6.6485102 -2.1266452 [49] -7.7655759 15.6812337
x7_power_3<-x7^3 x7_power_3
输出结果
[1] 33.9496856 -171.4503457 5259.5720057 4313.4446351 -6.3844187 [6] 11452.3277148 485.4373498 65.8224351 1112.0481567 -9.2386881 [11] 468.0130992 526.6112109 -34.7267073 844.8273724 -0.1214232 [16] -838.5136714 20.8451568 514.9648182 306.9329436 -0.5685154 [21] 19.9017160 3343.9365954 1739.0388326 -184.3343100 56.7904796 [26] 1695.4787867 -1609.6260698 287.8167592 -20.7265065 233.2110260 [31] -156.5714026 311.0660965 10487.1169231 4729.7485860 1.3561062 [36] -187.7864627 1.1462069 -41.0591968 -3.7920724 -8084.1908915 [41] 297.8634392 5049.5894784 430.6185016 9515.4983018 121.9344257 [46] 540.9847640 293.8820194 -9.6180080 -468.2966028 3856.0324360