Re: [R] summarize a vector

2012-08-10 Thread arun
r-help@r-project.org" ; "s...@gnu.org" ; Bert Gunter Sent: Friday, August 10, 2012 6:58 PM Subject: Re: [R] summarize a vector On Aug 10, 2012, at 3:42 PM, Michael Weylandt wrote: > I wouldn't be surprised if one couldn't get an *apply-free solution by using > dif

Re: [R] summarize a vector

2012-08-10 Thread Bert Gunter
Certainly ... but this is of course limited to the few C coded functions available. Back to apply-type stuff for, say, median as a summary statistic. -- Bert On Fri, Aug 10, 2012 at 3:58 PM, David Winsemius wrote: > > On Aug 10, 2012, at 3:42 PM, Michael Weylandt wrote: > >> I wouldn't be surpri

Re: [R] summarize a vector

2012-08-10 Thread David Winsemius
On Aug 10, 2012, at 3:42 PM, Michael Weylandt wrote: I wouldn't be surprised if one couldn't get an *apply-free solution by using diff(), cumsum() and selective indexing as well. What about colSums on a matrix extended with the right number of zeros. > colSums(matrix (c(v, rep(0, 3- length(

Re: [R] summarize a vector

2012-08-10 Thread Michael Weylandt
I wouldn't be surprised if one couldn't get an *apply-free solution by using diff(), cumsum() and selective indexing as well. Cheers, Michael On Aug 10, 2012, at 5:07 PM, David Winsemius wrote: > > On Aug 10, 2012, at 12:57 PM, Bert Gunter wrote: > >> ... or perhaps even simpler: >> >>> sz

Re: [R] summarize a vector

2012-08-10 Thread Bert Gunter
Oh yes, I stand corrected. I didn't look at your code carefully enough. -- Bert On Fri, Aug 10, 2012 at 3:07 PM, David Winsemius wrote: > > On Aug 10, 2012, at 12:57 PM, Bert Gunter wrote: > >> ... or perhaps even simpler: >> >>> sz <- function(x,k)tapply(x,(seq_along(x)-1)%/%k, sum) >>> sz(1:10

Re: [R] summarize a vector

2012-08-10 Thread Bert Gunter
Oh yes, I stand corrected. I didn't look at your code carefully enough. -- Bert On Fri, Aug 10, 2012 at 3:07 PM, David Winsemius wrote: > > On Aug 10, 2012, at 12:57 PM, Bert Gunter wrote: > >> ... or perhaps even simpler: >> >>> sz <- function(x,k)tapply(x,(seq_along(x)-1)%/%k, sum) >>> sz(1:10

Re: [R] summarize a vector

2012-08-10 Thread David Winsemius
On Aug 10, 2012, at 12:57 PM, Bert Gunter wrote: ... or perhaps even simpler: sz <- function(x,k)tapply(x,(seq_along(x)-1)%/%k, sum) sz(1:10,3) 0 1 2 3 6 15 24 10 Note that this works for k>n, where the previous solution does not. sz(1:10,15) 0 55 I agree that it is more elegant, but

Re: [R] summarize a vector

2012-08-10 Thread Sam Steingold
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 [2012-08-10 12:57:40 -0700]: > >> sz <- function(

Re: [R] summarize a vector

2012-08-10 Thread Bert Gunter
... or perhaps even simpler: > sz <- function(x,k)tapply(x,(seq_along(x)-1)%/%k, sum) > sz(1:10,3) 0 1 2 3 6 15 24 10 Note that this works for k>n, where the previous solution does not. > sz(1:10,15) 0 55 -- Bert On Fri, Aug 10, 2012 at 12:37 PM, David Winsemius wrote: > > On Aug 10, 201

Re: [R] summarize a vector

2012-08-10 Thread David Winsemius
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] I can, of course, iterate: w <- vector(mode="num