[R] Average every 4 columns

Jim Lemon drjimlemon at gmail.com
Wed Nov 9 23:00:38 CET 2016


Thanks - it made me realize that I had reversed the column and row selection

rowMeans(x[seq(1:dim(x)[1],by=4),-1])

Jim


On Thu, Nov 10, 2016 at 8:30 AM, Uwe Ligges
<ligges at statistik.tu-dortmund.de> wrote:
>
>
> On 09.11.2016 22:06, Jim Lemon wrote:
>>
>> Hi Milu,
>> Perhaps this will help:
>>
>> apply(as.matrix(x[-1,seq(1:dim(x)[1],by=4)]),1,mean)
>
>
> More efficient than apply(..., 1, mean): rowMeans()
>
> Best,
> Uwe Ligges
>
>
>
>
>>
>> Jim
>>
>>
>> On Thu, Nov 10, 2016 at 4:00 AM, Miluji Sb <milujisb at gmail.com> wrote:
>>>
>>> Thanks a lot for your quick reply. I made a mistake in the question, I
>>> meant to ask every 4 (or 12) rows not columns. Apologies. Thanks again!
>>>
>>> Sincerely,
>>>
>>> Milu
>>>
>>> On Wed, Nov 9, 2016 at 5:45 PM, Dalthorp, Daniel <ddalthorp at usgs.gov>
>>> wrote:
>>>
>>>> Hi Milu,
>>>> The following should work for an array x (provided dim(x)[2] is
>>>> divisible
>>>> by 4):
>>>>
>>>> colMeans(x[,0:(dim(x)[2]/4-1)*4+1])
>>>>
>>>> -Dan
>>>>
>>>> On Wed, Nov 9, 2016 at 8:29 AM, Miluji Sb <milujisb at gmail.com> wrote:
>>>>
>>>>> Dear all,
>>>>>
>>>>> I have a dataset with hundreds of columns, I am only providing only 12
>>>>> columns. Is it possible to take the mean of every four (or 12) columns
>>>>>  (value601, value602, value603, value604 etc.in this case) and repeat
>>>>> for
>>>>> the hundreds of columns? Thank you.
>>>>>
>>>>> Sincerely,
>>>>>
>>>>> Milu
>>>>>
>>>>> structure(list(value601 = c(10.1738710403442, 3.54112911224365,
>>>>> 12.9192342758179, 3.17447590827942, 11.7332258224487, 7.68282270431519,
>>>>> -7.11564493179321, 0.987620949745178, 13.0476207733154,
>>>>> 6.36939525604248
>>>>> ), value602 = c(13.0642414093018, 5.53129482269287, 16.0519638061523,
>>>>> 2.88946437835693, 14.9204912185669, 9.42428588867188,
>>>>> -6.80674123764038,
>>>>> -0.614241063594818, 16.7947769165039, 7.9541072845459), value603 =
>>>>> c(22.0399188995361,
>>>>> 14.398024559021, 24.9523792266846, 12.0878629684448, 23.6459674835205,
>>>>> 18.3277816772461, -2.54092741012573, 10.5550804138184,
>>>>> 25.1016540527344,
>>>>> 16.2166938781738), value604 = c(27.7165412902832, 20.3255825042725,
>>>>> 30.8430004119873, 16.6856250762939, 29.2485408782959, 24.3775005340576,
>>>>> 6.47758340835571, 15.5897912979126, 30.7387924194336, 22.3637084960938
>>>>> ), value605 = c(31.6644763946533, 23.4093952178955, 35.1488723754883,
>>>>> 19.7132263183594, 33.3924179077148, 29.5846366882324, 10.2083873748779,
>>>>> 19.3551616668701, 35.3076629638672, 27.4299201965332), value606 =
>>>>> c(33.9698333740234,
>>>>> 26.8574161529541, 36.8900833129883, 22.8604583740234, 34.8642921447754,
>>>>> 33.8158760070801, 14.7055835723877, 22.1144580841064, 37.0545425415039,
>>>>> 32.1087913513184), value607 = c(36.0279846191406, 26.9297180175781,
>>>>> 38.2701225280762, 23.2643146514893, 36.7398796081543, 34.1216125488281,
>>>>> 17.1387901306152, 24.0419750213623, 37.8542327880859, 32.7677421569824
>>>>> ), value608 = c(34.0242347717285, 25.7720966339111, 36.4897193908691,
>>>>> 22.0332260131836, 34.8011703491211, 32.6856842041016, 16.6232261657715,
>>>>> 21.5571365356445, 36.1491546630859, 31.1716938018799), value609 =
>>>>> c(27.5402088165283,
>>>>> 21.7590408325195, 30.5214176177979, 18.4252090454102, 29.1156253814697,
>>>>> 26.9878330230713, 12.4962501525879, 17.7259578704834, 30.9099159240723,
>>>>> 25.4832077026367), value610 = c(23.4706859588623, 17.0126209259033,
>>>>> 26.8166942596436, 15.297459602356, 25.1733055114746, 23.5616931915283,
>>>>> 8.86995983123779, 13.5793552398682, 27.5732250213623, 22.1691932678223
>>>>> ), value611 = c(14.5820417404175, 9.08279132843018, 17.8419170379639,
>>>>> 8.36016654968262, 16.5633754730225, 14.8123331069946, 1.32095837593079,
>>>>> 5.73408317565918, 18.9752082824707, 13.5729999542236), value612 =
>>>>> c(9.12979793548584,
>>>>> 2.79943537712097, 11.6504030227661, 2.21584677696228, 10.5404834747314,
>>>>> 7.55471754074097, -5.58141136169434, -0.566209673881531,
>>>>> 12.3264112472534,
>>>>> 6.65576601028442)), .Names = c("value601", "value602", "value603",
>>>>> "value604", "value605", "value606", "value607", "value608", "value609",
>>>>> "value610", "value611", "value612"), row.names = c("1", "2",
>>>>> "3", "4", "5", "6", "7", "8", "9", "10"), class = "data.frame")
>>>>>
>>>>>         [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide http://www.R-project.org/posti
>>>>> ng-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Dan Dalthorp, PhD
>>>> USGS Forest and Rangeland Ecosystem Science Center
>>>> Forest Sciences Lab, Rm 189
>>>> 3200 SW Jefferson Way
>>>> Corvallis, OR 97331
>>>> ph: 541-750-0953
>>>> ddalthorp at usgs.gov
>>>>
>>>>
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>



More information about the R-help mailing list