[R] Feed rle() output to hist()

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Tue Mar 26 22:09:23 CET 2013


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Newbie1234
> Sent: Tuesday, March 26, 2013 2:03 PM
> To: r-help at r-project.org
> Subject: [R] Feed rle() output to hist()
> 
> I want to make a histogram from the lengths vector which is part of the
> output of rle.  But I don't know how to access that vector so that I
> use it
> as an argument of hist().  What argument must I use so that I use the
> lengths vector as an input to hist()?
> 
> Example output is:
> 
> Run Length Encoding
>   lengths: int [1:4] 1 2 3 3
>   values : num [1:4] -1 1 -1 1
> 
> A printout of the function rle() may give some clues, however - not
> enough
> for me.  Here is the print out of rle()
> 
> > rle
> function (x)
> {
>     if (!is.vector(x) && !is.list(x))
>         stop("'x' must be an atomic vector")
>     n <- length(x)
>     if (n == 0L)
>         return(structure(list(lengths = integer(), values = x),
>             class = "rle"))
>     y <- x[-1L] != x[-n]
>     i <- c(which(y | is.na(y)), n)
>     structure(list(lengths = diff(c(0L, i)), values = x[i]),
>         class = "rle")
> }
> <bytecode: 0x7fc7060a52d8>
> <environment: namespace:base>
> 

If you have

  runs <- rle(sign(zdiff))

then look at 

  str(runs)

Then maybe this does what you want

  hist(runs$lengths)



Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list