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()> rlefunction (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> -- View this message in context: http://r.789695.n4.nabble.com/Feed-rle-output-to-hist-tp4662554.html Sent from the R help mailing list archive at Nabble.com.
> -----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
Do you want a histogram of the length distribution, or a barplot of the actual lengths? Either way, myrle$lengths will get it, if myrle is the output of rle(). ?rle tells you that: ?rle()? returns an object of class ?"rle"? which is a list with components: lengths: an integer vector containing the length of each run. values: a vector of the same length as ?lengths? with the corresponding values. If you don't understand what list components are, you should certainly read Introduction to R. Looking at the help is always a better place to start than looking at the code, though the last line of the code shows exactly the same thing. Here's an example: x <- c(rev(rep(6:10, 1:5)), 5, rep(6:10, 1:5)) myrle <- rle(x) hist(myrle$lengths) barplot(myrle$lengths) Sarah On Tue, Mar 26, 2013 at 5:02 PM, Newbie1234 <larry at riskengineer.com> wrote:> 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> > >-- Sarah Goslee http://www.functionaldiversity.org