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@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@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.
Re: [R] 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.00 0.00 0.00 0.00 0.00 0.00 0.00 8 9 10 11 0.146375 0.00 0.194500 100.00 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.00 0.00 0.00 0.00 0.00 0.00 0.00 [8] 0.146375 0.00 0.194500 10.00 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 11 0.00 22 0.00 33 0.00 44 0.00 55 0.00 66 0.00 77 0.00 88 0.146375 99 0.00 10 10 0.194500 11 11 100.00 -- David L Carlson Associate Professor of Anthropology Texas AM University College Station, TX 77843-4352 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@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@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@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@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.
Re: [R] summarise subsets of a vector
Or... fs-rep(1:ceiling(length(test)/10),each=10)[1:length(test)] result-by(test,fs,mean) which will get you the version with the 101st as a single datapoint so many possibilities.. On 23.01.2013, at 16:43, David L Carlson wrote: 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.00 0.00 0.00 0.00 0.00 0.00 0.00 8 9 10 11 0.146375 0.00 0.194500 100.00 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.00 0.00 0.00 0.00 0.00 0.00 0.00 [8] 0.146375 0.00 0.194500 10.00 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 11 0.00 22 0.00 33 0.00 44 0.00 55 0.00 66 0.00 77 0.00 88 0.146375 99 0.00 10 10 0.194500 11 11 100.00 -- David L Carlson Associate Professor of Anthropology Texas AM University College Station, TX 77843-4352 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@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@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@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@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] 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@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.
Re: [R] summarise subsets of a vector
Hi, try this: unlist(lapply(split(test,((seq_along(test)-1)%/% 10)+1),mean)) # 1 2 3 4 5 6 7 8 #0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.146375 # 9 10 11 #0.00 0.194500 0.00 A.K. - Original Message - From: Wim Kreinen wkrei...@gmail.com To: r-help r-help@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@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@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.