Re: [R] mean duration (persistence) of daily data

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sun, 20 Jun 2010 21:12:34 -0400

On Jun 20, 2010, at 5:52 PM, Schmidt Martin wrote:

> Dear R users
>
> A quite simple question: how can I get the mean duration
> (persistence) of daily data, when the data set looks as follows:
>
> [1] 7 7 7 9 9 5 7 5 5 5 9 5 5 6 6 6 1 1 1 2 2 4 4 4 4 7 7 3 3 2 4 4
> 7 7 7 7 7
> [38] 7 7 5 5 9 9 5 5 5 1 7 9 9 9 9 9 5 5 5 5 5 3 4 4 4 4 4 4 4 4 2 2
> 2 2 2 2 8
> [75] 8 6 6 6 8 8 8 8 8 6 6 2 8 8 8 8 8 8 8 8 8 2 2 2 2 2 6 6 6 6 6 6
> 6 6 6 6 3
> [112] 3 2 2 2 1 5 1 1 4 2 2 2 2 2 2 2 3 4 4 4 3 3 3 6 6 6 8 8 4 4 2
> 4 4 1 1 6 6
> .............................and so on:
>
> Thus, I'd like to know, from each number (1-9) the mean duration
> from the first to the last day (here not visible, but each number
> stands for a day).
>
> With:
>
> length(which(test==1))
>
> I certainly get the total number of number 1......unfortunately
> that's all I got!

I am guessing you want:

?rle

 > dur <- scan()
1: 7 7 7 9 9 5 7 5 5 5 9 5 5 6 6 6 1 1 1 2 2 4 4 4 4 7 7 3 3 2 4 4 7 7 7 7 7
38: 7 7 5 5 9 9 5 5 5 1 7 9 9 9 9 9 5 5 5 5 5 3 4 4 4 4 4 4 4 4 2 2 2 2 2 2 8
75: 8 6 6 6 8 8 8 8 8 6 6 2 8 8 8 8 8 8 8 8 8 2 2 2 2 2 6 6 6 6 6 6 6 6 6 6 3
112: 3 2 2 2 1 5 1 1 4 2 2 2 2 2 2 2 3 4 4 4 3 3 3 6 6 6 8 8 4 4 2 4 4 1 1 6 6
149:
Read 148 items
 > rle(dur)
Run Length Encoding

   lengths: int [1:51] 3 2 1 1 3 1 2 3 3 2 ...    values : num [1:51] 7 9 5 7 5 9 5 6 1 2 ...  > mean(rle(dur)$lengths)
[1] 2.901961

-- 
Just a guess, mind you. Your problem description seemed vague in both  
the input ahd the output sides.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 21 Jun 2010 - 01:17:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jun 2010 - 01:40:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive