[R] summarize a vector

Sam Steingold sds at gnu.org
Fri Aug 10 23:34:41 CEST 2012


Thanks David & Bert.
It turned out that what I actually wanted was much simpler.
my vector's elements are 0&1 and the right way to "summarize" it is
hist(which(v==1))
however, your replies were quire educational!
Thanks again,
Sam.

> * Bert Gunter <thagre.oregba at trar.pbz> [2012-08-10 12:57:40 -0700]:
>
>> sz <- function(x,k)tapply(x,(seq_along(x)-1)%/%k, sum)
>
> On Fri, Aug 10, 2012 at 12:37 PM, David Winsemius
> <dwinsemius at comcast.net> wrote:
>>
>> On Aug 10, 2012, at 12:20 PM, Sam Steingold wrote:
>>
>>> 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]
>>
>>> w <- tapply( v ,rep(1:(N/k +1), each=k, len=N ) , sum)

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://camera.org http://iris.org.il
http://palestinefacts.org http://dhimmi.com http://truepeace.org
At war time "salt of the earth" becomes "cannon fodder".



More information about the R-help mailing list