I have been searching around and trying multiple different approaches to average every 10th column in a data.frame
. The data set is 52 rows x 60 columns. The data.frame
, titled data
, looks like this for the first 2 rows:
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30 X31 X32 X33 X34 X35 X36 X37 X38 X39 X40 X41 X42 X43 X44 X45 X46 X47 X48 X49 X50 X51 X52 X53 X54 X55 X56 X57 X58 X59 X60
4 14.7637 14.2117 14.1237 13.6637 12.9837 13.3237 13.8877 15.0997 15.5717 16.5157 15.0597 13.5317 13.6957 13.2637 13.5117 13.4237 14.1277 13.8437 12.8357 13.6277 13.2077 14.9837 16.1277 15.6197 15.7517 16.8557 15.9757 15.9677 16.1677 17.1557 16.1157 16.3557 16.2037 16.8077 16.6757 16.4837 16.7877 16.1037 16.3117 16.0637 16.1077 16.2477 17.1917 18.1236 18.5036 18.2956 20.9516 18.0636 18.5516 19.1756 19.5996 19.2036 18.1996 16.7117 16.7037 16.7877 16.5837 17.6636 18.8596 18.3356
5 16.9597 15.9037 15.3917 15.6797 15.6797 15.8397 17.1517 18.0796 18.6236 20.4796 18.8796 16.2877 16.7997 15.6157 16.9917 16.8317 16.9917 17.5356 16.3517 15.1357 16.5437 17.4077 18.4316 17.0557 17.3117 19.1676 18.2396 16.7037 17.2157 19.1676 18.2076 16.7677 18.7196 19.4236 18.2716 17.5356 18.7196 17.8876 17.2477 16.9597 17.2797 18.3996 19.5516 19.2636 20.0956 20.4476 21.5356 18.4316 20.7356 22.1436 21.6636 20.7676 19.7436 18.5596 17.9516 17.8876 18.1116 19.2956 20.3516 19.4876
(The 4 and 5 as well as the top row are just placeholders in the file.
The data is being read and pulled from a .txt
file and I want to average every 10th column to change it from 60 columns into 6. Here is some extra information that I have seen people ask for previously:
> class(data)
[1] "data.frame"
> str(data)
'data.frame': 52 obs. of 60 variables:
$ X1 : Factor w/ 53 levels "0","0.0319994",..: 31 32 34 30 51 48 45 39 36 28 ...
$ X2 : Factor w/ 48 levels "0","0.0319994",..: 27 30 29 26 46 42 39 31 23 19 ...
Most recently I have tried:
dataMean <- data.frame(Means=rowMeans(data), ncol=10)
and
dataMean <- rowMeans(data.frame(data, ncol=10))
and both give the same error about 'x' must be numeric.Any help that someone could provide will be appreciated.
Thanks in Advance!
Edit: The desired results would be something like this where the number of columns has be reduced and the arithmetic mean is calculated for every 10 columns:
X1 X2 X3 X4 X5 X6
4 14.4145 13.6921 15.7813 16.3909 18.12123 17.86484
5 16.97887 16.74208 17.72446 17.97403 19.78841 19.382
Edit2:
> dput(df)
structure(list(X1X2X3X4X5X6X7X8X9X10X11X12X13X14X15X16X17X18X19X20X21X22X23X24X25X26X27X28X29X30X31X32X33X34X35X36X37X38X39X40X41X42X43X44X45X46X47X48X49X50X51X52X53X54X55X56X57X58X59X60 = c("414.763714.211714.123713.663712.983713.323713.887715.099715.571716.515715.059713.531713.695713.263713.511713.423714.127713.843712.835713.627713.207714.983716.127715.619715.751716.855715.975715.967716.167717.155716.115716.355716.203716.807716.675716.483716.787716.103716.311716.063716.107716.247717.191718.123618.503618.295620.951618.063618.551619.175619.599619.203618.199616.711716.703716.787716.583717.663618.859618.3356",
="516.959715.903715.391715.679715.679715.839717.151718.079618.623620.479618.879616.287716.799715.615716.991716.831716.991717.535616.351715.135716.543717.407718.431617.055717.311719.167618.239616.703717.215719.167618.207616.767718.719619.423618.271617.535618.719617.887617.247716.959717.279718.399619.551619.263620.095620.447621.535618.431620.735622.143621.663620.767619.743618.559617.951617.887618.111619.295620.351619.4876"
)), class = "data.frame", row.names = c(NA, -2L))