[R] grouping

K. Elo maillists at nic.fi
Tue Apr 3 15:28:33 CEST 2012


Hi!

Maybe not the most elegant solution, but works:

for(i in seq(1,length(data)-(length(data) %% 3), 3)) { 
ifelse((length(data)-i)>3, { print(sort(data)[ c(i:(i+2)) ]); 
print(mean(sort(data)[ c(i:(i+2)) ])) }, { print(sort(data)[ 
c(i:length(data)) ]); print(mean(sort(data)[ c(i:length(data)) ])) } ) }

Produces:

[1] 36 45 46
[1] 42.33333
[1]  66  78 125
[1] 89.66667
[1] 193 209 242 297
[1] 235.25

HTH,
Kimmo



More information about the R-help mailing list