计算R中矩阵中列数据的平均值

时间:2013-07-22 09:56:32

标签: r

有63x14矩阵数据。我想从最后一个col到第一个col取每个平均三列。我写了如下脚本。但它显示如下错误。它也不是以给定的矩阵格式保存。由于我是R的初学者,我总是得到你网站的帮助。请帮我解决这个问题。

  mydata <- read.table ("SAT.txt", header =T)
    SAT<- matrix(nrow=63, ncol=12)
    for (i in 3:ncol(mydata)){
    SAT [i] <- print(cbind(rowMeans(mydata[, c(i-2, i-1, i)], na.rm=TRUE)))
     }

 I got the results as follows : means 12 columns separately with following eeror.

          [,1]
 [1,] 25.35300
 [2,] 25.88500
 [3,] 25.52600
 [4,] 25.84233
 [5,] 25.59233
 [6,] 26.97967
 [7,] 26.83733
 [8,] 26.77567
 [9,] 27.06967
[10,] 27.16967
[11,] 27.08500
[12,] 27.26400
[13,] 26.92500
[14,] 26.31033
[15,] 26.11833
[16,] 27.30233
[17,] 26.89600
[18,] 27.80567
[19,] 27.17367
[20,] 27.12767
[21,] 27.79767
[22,] 27.65067
[23,] 26.87933
[24,] 26.96333
[25,] 27.80600
[26,] 26.93933
[27,] 27.37767
[28,] 27.00033
[29,] 26.99933
[30,] 27.63000
[31,] 27.73900
[32,] 27.67167
[33,] 27.47933
[34,] 27.59167
[35,] 28.05467
[36,] 27.56400
[37,] 27.61800
[38,] 27.54367
[39,] 27.74200
[40,] 28.03567
[41,] 27.48467
[42,] 27.66933
[43,] 27.47067
[44,] 27.55567
[45,] 27.07733
[46,] 27.14567
[47,] 27.15400
[48,] 26.94767
[49,] 27.23067
[50,] 28.04300
[51,] 27.31033
[52,] 27.08900
[53,] 27.46600
[54,] 27.09833
[55,] 27.43300
[56,] 27.33200
[57,] 27.20233
[58,] 27.15600
[59,] 27.19133
[60,] 26.83700
[61,] 27.38133
[62,] 27.59300
[63,] 26.55533
          [,1]
 [1,] 25.70100
 [2,] 26.09433
 [3,] 25.81733
 [4,] 26.13433
 [5,] 25.92800
 [6,] 27.37500
 [7,] 27.35067
 [8,] 27.12900
 [9,] 27.51767
[10,] 27.52733
[11,] 27.54400
[12,] 27.54700
[13,] 27.20433
[14,] 26.58467
[15,] 26.70367
[16,] 27.57600
[17,] 27.53700
[18,] 28.01667
[19,] 27.63967
[20,] 27.74400
[21,] 28.19067
[22,] 28.06200
[23,] 27.46367
[24,] 27.57300
[25,] 28.20367
[26,] 27.54100
[27,] 27.81300
[28,] 27.46167
[29,] 27.76433
[30,] 28.07767
[31,] 28.19133
[32,] 28.21733
[33,] 28.05500
[34,] 28.11267
[35,] 28.45700
[36,] 28.04133
[37,] 28.09800
[38,] 28.16167
[39,] 28.26433
[40,] 28.36733
[41,] 27.96567
[42,] 28.25667
[43,] 27.94500
[44,] 28.01900
[45,] 27.69733
[46,] 27.58667
[47,] 27.82067
[48,] 27.60967
[49,] 27.94700
[50,] 28.32667
[51,] 27.70833
[52,] 27.64967
[53,] 27.88000
[54,] 27.74067
[55,] 27.83500
[56,] 28.12333
[57,] 27.89733
[58,] 27.57400
[59,] 27.65767
[60,] 27.43433
[61,] 27.95067
[62,] 27.98367
[63,] 27.15133
          [,1]
 [1,] 25.76533
 [2,] 26.07900
 [3,] 25.80667
 .
 .

错误;

Warning messages:

    1: In SAT[i] <- print(cbind(rowMeans(mydata[, c(i - 2, i -  ... :
      number of items to replace is not a multiple of replacement length
    2: In SAT[i] <- print(cbind(rowMeans(mydata[, c(i - 2, i -  ... :
      number of items to replace is not a multiple of replacement length
    3: In SAT[i] <- print(cbind(rowMeans(mydata[, c(i - 2, i -  ... :
      number of items to replace is not a multiple of replacement length
    4:
    .
    .
    12: 

如何将这些独立的12列组合成63 x 12矩阵?

非常感谢

1 个答案:

答案 0 :(得分:1)

我认为你有一个编码兔子洞。你在那里有太多的功能,真的没有为你做任何事情。你不需要打印。您正在尝试将矩阵存储到矩阵中,而不指定有意义/正确的索引值等。我真的建议您查看一些介绍性的R文本以熟悉索引,这似乎是您的主要问题。

简单来说,我认为你只想要这样的东西:

out <- sapply(3:ncol(mydata), function(i) {
            rowMeans(mydata[, c(i-2, i-1, i)], na.rm=TRUE)
       })