计算每行平均值的偏差

时间:2013-03-01 07:38:10

标签: r row mean deviation

使用下面的代码,我编写了一个函数,它给出了每行(1)中值的平均值。现在我想计算第i行中每个值(i,j)与第i行计算的平均值(使用函数)的偏差。我在下面写了一个函数(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)
}

4 个答案:

答案 0 :(得分:3)

您无需自行创作。只需使用函数rowMeans()计算行均值,并使用这些值来偏离这些均值。

Z<-matrix(sample(1:40),ncol=4)
Z
      [,1] [,2] [,3] [,4]
 [1,]   32   19   35    4
 [2,]   11   31   33   38
 [3,]   15   29    2    8
 [4,]   18   34    5    3
 [5,]   21   24   39   10
 [6,]    9   16   27   30
 [7,]   20    1   37   17
 [8,]   22   23   25   40
 [9,]   12    7   26    6
[10,]   13   28   36   14

rowMeans(Z)
 [1] 22.50 28.25 13.50 15.00 23.50 20.50 18.75 27.50 12.75 22.75

Z-rowMeans(Z)
        [,1]   [,2]   [,3]   [,4]
 [1,]   9.50  -3.50  12.50 -18.50
 [2,] -17.25   2.75   4.75   9.75
 [3,]   1.50  15.50 -11.50  -5.50
 [4,]   3.00  19.00 -10.00 -12.00
 [5,]  -2.50   0.50  15.50 -13.50
 [6,] -11.50  -4.50   6.50   9.50
 [7,]   1.25 -17.75  18.25  -1.75
 [8,]  -5.50  -4.50  -2.50  12.50
 [9,]  -0.75  -5.75  13.25  -6.75
[10,]  -9.75   5.25  13.25  -8.75

答案 1 :(得分:1)

你可以使用apply以更“R like”的方式思考你的循环。

( Z <- data.frame(V1=runif(28,0,100), V2=runif(28,0,100), 
                V3=runif(28,0,100), V4=runif(28,0,100)) )

( Z.dev <- Z - apply(Z, MARGIN=1, FUN=mean) )

答案 2 :(得分:0)

你可以使用scale来获得列偏差(因为它执行居中),所以只需在转置上使用它,然后再转置以获得所需的输出:

x <- matrix(sample(1:12),4)
x
     [,1] [,2] [,3]
[1,]    6    4    1
[2,]   12    3    7
[3,]    9   10   11
[4,]    2    5    8
t(scale(t(x),scale=F))
          [,1]       [,2]       [,3]
[1,]  2.333333  0.3333333 -2.6666667
[2,]  4.666667 -4.3333333 -0.3333333
[3,] -1.000000  0.0000000  1.0000000
[4,] -3.000000  0.0000000  3.0000000
attr(,"scaled:center")
[1]  3.666667  7.333333 10.000000  5.000000

center属性给出了行的意思。

答案 3 :(得分:0)

您可以使用sweeprowMeans,如下所示:

> set.seed(1) # for the sample to be reproducible
> x <- matrix(sample(1:12),4)
> sweep(x, 1, rowMeans(x))
           [,1]      [,2]      [,3]
[1,]  1.0000000 -1.000000  0.000000
[2,]  0.6666667  2.666667 -3.333333
[3,] -3.0000000  1.000000  2.000000
[4,] -0.6666667  2.333333 -1.666667

有关详细信息,请参阅?sweep