[R] summarize a vector

Sam Steingold sds at gnu.org
Fri Aug 10 21:20:57 CEST 2012


I have a long numeric vector v (length N) and I want create a shorter
vector of length N/k consisting of sums of k-subsequences of v:

v <- c(1,2,3,4,5,6,7,8,9,10)

N=10, k=3
===> [6,15,24,10]

I can, of course, iterate: 

> w <- vector(mode="numeric",length=ceiling(N/k))
> for (i in 1:length(w)) w[i] <- sum(v(i*k:(i+1)*k))

(modulo boundary conditions)
but I wonder if there is a better way.

thanks!

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://think-israel.org http://thereligionofpeace.com
http://dhimmi.com http://truepeace.org http://www.PetitionOnline.com/tap12009/
Type louder, please.



More information about the R-help mailing list