[R] summarise subsets of a vector

arun smartpink111 at yahoo.com
Wed Jan 23 00:39:56 CET 2013


Hi,
try this:
 unlist(lapply(split(test,((seq_along(test)-1)%/% 10)+1),mean))
#       1        2        3        4        5        6        7        8 
#0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.146375 
  #     9       10       11 
#0.000000 0.194500 0.000000 

A.K.



----- Original Message -----
From: Wim Kreinen <wkreinen at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Tuesday, January 22, 2013 6:09 PM
Subject: [R] summarise subsets of a vector

Hello,

I have vector called test. And now I wish to measure the mean of the first
10 number, the second 10 numbers etc
How does it work?
Thanks Wim

> dput (test)
c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.71, 0.21875, 0, 0.27375, 0.26125,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.84125,
0.0575, 0.92625, 0.12, 0, 0)

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
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