s-news
[Top] [All Lists]

Re: running sum of counts

To: Tim Wade <wade_tj@yahoo.com>
Subject: Re: running sum of counts
From: Sundar Dorai-Raj <sundar.dorai-raj@PDF.COM>
Date: Mon, 19 Apr 2004 13:46:38 -0500
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <20040419181637.93727.qmail@web12307.mail.yahoo.com>
Organization: PDF Solutions, Inc.
References: <20040419181637.93727.qmail@web12307.mail.yahoo.com>
Reply-to: sundar.dorai-raj@PDF.COM
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)


Tim Wade wrote:

Hello:

I have what I'm sure is a very simple question.

I have a vector like this:

1 1 1 1 2 2 2 3 3 4 4 4 4 4 5 5 5 5 5 6 7 3 3 3 2 2.

I would like to generate a sequence consisting of a
running sum of count values, like this:

1 2 3 4 1 2 3 1 2 1 2 3 4 5 1 2 3 4 5 1 1 1 2 3 1 1.

Thanks very much for any assistance!

Tim


Hi Tim,

I'm assuming the last number in the second sequence should really be a 2 and not a one. My solution isn't pretty, but it does the job:

> x
 [1] 1 1 1 1 2 2 2 3 3 4 4 4 4 4 5 5 5 5 5 6 7 3 3 3 2 2
> unlist(sapply(diff(c(0, which(diff(c(x, Inf)) != 0))), seq))
 [1] 1 2 3 4 1 2 3 1 2 1 2 3 4 5 1 2 3 4 5 1 1 1 2 3 1 2

--sundar



<Prev in Thread] Current Thread [Next in Thread>