s-news
[Top] [All Lists]

running sum of counts, summary

To: s-news@lists.biostat.wustl.edu
Subject: running sum of counts, summary
From: Tim Wade <wade_tj@yahoo.com>
Date: Tue, 20 Apr 2004 07:22:35 -0700 (PDT)
Thanks all who responded to my query. I received
several very quick replies! Most of the suggestions
involved using the rle function with sapply or lapply.


Tim

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

It does sound simple, doesn't it?  Here's a function
that does the 
trick:

counts <- function(x) {
      L <- length(x)
      first.new <- c(1, 1 - (x[-1] == x[ - L]))
      group <- cumsum(first.new)
      as.vector(unlist(tapply(x, group, function(i)
seq(along=i))))
      }

counts(a)
 [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

I'd be curious if you get a more elegant response.

JVA



Hi Tim:

Have you tried rle()? 

rle(c(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))
$lengths:
[1] 4 3 2 5 5 1 1 3 2

$values:
[1] 1 2 3 4 5 6 7 3 2


- HTH

Shawn Boles, Ph.D.

 
unlist(lapply(rle(x)$lengths, seq))

There are two parts to this:
(1)
        rle(x)$lengths
counts the repetitions.

(2) unlist(lapply(  y  , seq))
converts c(4, 3, ...)
into c(1:4, 1:3, ...)

Tim Hesterberg




=====
wade_tj@yahoo.com


        
                
__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
http://photos.yahoo.com/ph/print_splash

<Prev in Thread] Current Thread [Next in Thread>
  • running sum of counts, summary, Tim Wade <=