Calculate average of columns data in a matrix in R -


there 63x14 matrix data. want take average of 3 column each last col first col. wrote script below. shows errors follows. not save in given matrix format. since i'm beginner r, got site always. please me solve problem.

  mydata <- read.table ("sat.txt", header =t)     sat<- matrix(nrow=63, ncol=12)     (i in 3:ncol(mydata)){     sat [i] <- print(cbind(rowmeans(mydata[, c(i-2, i-1, i)], na.rm=true)))      }   got results follows : means 12 columns separately 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  .  . 

error;

warning messages:      1: in sat[i] <- print(cbind(rowmeans(mydata[, c(i - 2, -  ... :       number of items replace not multiple of replacement length     2: in sat[i] <- print(cbind(rowmeans(mydata[, c(i - 2, -  ... :       number of items replace not multiple of replacement length     3: in sat[i] <- print(cbind(rowmeans(mydata[, c(i - 2, -  ... :       number of items replace not multiple of replacement length     4:     .     .     12:  

how can combine separate 12 columns 63 x 12 matrix?

thanks lot

i think have down bit of coding rabbit hole. you've got way many functions in there aren't doing you. don't need print. you're trying store matrix matrix, without specifying meaningful/correct index values, etc. i'd recommend taking @ introductory r texts familiar indexing, seems main issue here.

in simpler terms, think want like:

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

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -