次のコードを使用して、各行の値の平均を求める関数を作成しました (1)。ここで、行 i で計算された平均値から行 i の各値 (i,j) の偏差を計算したいと思います (関数を使用)。以下に関数 (2) を書きましたが、機能していないようです。誰か助けてくれませんか?
(1) n=28
k=4
Avg <- function (n,k) {
for (i in 1:28) {
AverageGrades[i]=((rowSums(Z)[i])/k)
}
return(AverageGrades)
}
AverageGrades=Avg(28,4)
AverageGrades
どこ
Z is:
> Z
V1 V2 V3 V4
[1,] 77.81146 80.37801 72.33208 77.73541
[2,] 70.38343 62.33750 67.74083 71.18287
[3,] 69.03035 74.43367 77.87572 77.89755
[4,] 83.05206 83.07390 71.76214 80.16890
[5,] 70.61465 60.07529 59.31726 69.93781
[6,] 72.22979 59.44618 74.65016 68.75547
[7,] 75.28208 79.15410 81.72065 73.67472
[8,] 94.78838 88.89368 73.35592 84.79245
[9,] 78.00313 66.96430 78.79857 95.90012
[10,] 66.70869 81.91267 76.01797 60.48021
[11,] 69.98514 54.44738 65.88391 64.10529
[12,] 80.21977 78.44115 74.95861 78.83063
[13,] 87.17776 76.04111 77.99261 74.81652
[14,] 75.67206 68.03770 56.90106 58.85256
[15,] 68.63858 79.25913 75.31810 82.69422
[16,] 65.40212 77.23639 94.33794 86.70359
[17,] 66.59640 75.00316 63.96433 75.79860
[18,] 65.77463 73.59685 63.05748 62.29946
[19,] 78.46871 77.71069 88.33124 84.39021
[20,] 71.37807 78.75419 65.97058 81.17457
[21,] 72.17469 75.61673 75.63857 64.32681
[22,] 65.97012 62.48757 66.35959 68.92614
[23,] 86.51898 82.57795 89.95407 77.17046
[24,] 77.95162 79.90312 76.72703 84.54925
[25,] 83.10545 79.92936 87.75158 77.21221
[26,] 69.64127 81.07779 79.29918 75.81663
[27,] 81.02364 69.71188 78.11864 67.07981
[28,] 71.42319 88.52474 80.89039 69.75374
(2) Deviation= function (n,k){
for(i in 1:28){
AverageGrades[i]=((rowSums(Z)[i])/k)
}
return(AverageGrades)
}
{
for (j in 1:4) {
dev[i,j]=(Z[i,j]-AverageGrades[i])
}
return(dev)
}