summarise subsets of a vector
You didn't indicate what you want to do with the 101st observation. Arun's solution creates an 11th group and divides by the number in the group(1), while Jessica's solution creates an 11th column and divides by 10.
test[101] <- 100 unlist(lapply(split(test,((seq_along(test)-1)%/% 10)+1),mean))
1 2 3 4 5 6 7
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8 9 10 11
0.146375 0.000000 0.194500 100.000000
x<-matrix(test,nrow=10)
Warning message: In matrix(test, nrow = 10) : data length [101] is not a sub-multiple or multiple of the number of rows [10]
apply(x,2,mean)
[1] 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 [8] 0.146375 0.000000 0.194500 10.000000 Another approach would be to use aggregate:
Groups <- gl(ceiling(length(test)/10), 10)[1:length(test)] aggregate(test, list(Groups), mean)
Group.1 x 1 1 0.000000 2 2 0.000000 3 3 0.000000 4 4 0.000000 5 5 0.000000 6 6 0.000000 7 7 0.000000 8 8 0.146375 9 9 0.000000 10 10 0.194500 11 11 100.000000 ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Jessica Streicher Sent: Wednesday, January 23, 2013 6:13 AM To: Wim Kreinen Cc: r-help Subject: Re: [R] summarise subsets of a vector Or maybe x<-matrix(test,nrow=10) apply(x,2,mean) On 23.01.2013, at 00:09, Wim Kreinen wrote:
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.
______________________________________________ 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.