[R] Code to fetch summary info from vector

Benjamin Gillespie gybrg at leeds.ac.uk
Tue Jan 15 18:19:05 CET 2013


Thanks everyone,

I've used the code Will supplied - this worked well.

Thanks to all the others who contributed.

Ben Gillespie
Research Postgraduate

School of Geography
University of Leeds
Leeds
LS2 9JT

Tel: +44(0)113 34 33345
Mob: +44(0)770 868 7641
http://www.geog.leeds.ac.uk/
________________________________________
From: William Dunlap [wdunlap at tibco.com]
Sent: 15 January 2013 16:51
To: Benjamin Gillespie; r-help at r-project.org
Subject: RE: Code to fetch summary info from vector

I don't completely understand the  question, but if you are looking
for the lengths of the runs of values greater than 1 then rle() would
help:
  > b <- c(1,1,1,2,3,4,3,2,1,1,1,1,1,2,3,4,5,4,3.5,3,2,1,1,1)
  > r <- rle(b>1)
  > r
  Run Length Encoding
    lengths: int [1:5] 3 5 5 8 3
    values : logi [1:5] FALSE TRUE FALSE TRUE FALSE
  > r$lengths[r$values]
  [1] 5 8
To get the maximum of each run of values greater than one, something
like the following may do (there are more elegant ways, but I don't want
to spend the time on it until I know what the question is):
  > runNumber <- cumsum( c(b[1]>1, (b[-1]>1) & (b[-length(b)]<=1)) )
  > runNumber[b<=1] <- NA
  > tapply(b, runNumber, max)
  1 2
  4 5

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Benjamin Gillespie
> Sent: Tuesday, January 15, 2013 8:16 AM
> To: r-help at r-project.org
> Subject: [R] Code to fetch summary info from vector
>
> Hi all,
>
> Thanks in advance for any help.
>
> I have a vector "b":
>
> b=c(1,1,1,2,3,4,3,2,1,1,1,1,1,2,3,4,5,4,3.5,3,2,1,1,1)
>
> Imagine b is river flow throughout time.
>
> I would like some code that will generate the following information:
>
> number of individual 'periods' where b>1 (= 2 in this case)
> period 1 length = 5, max = 4
> period 2 length = 8, max = 5
>
> I can't figure anything useful out.
>
> Thanks,
>
> Ben Gillespie
> Research Postgraduate
>
> School of Geography
> University of Leeds
> Leeds
> LS2 9JT
>
> http://www.geog.leeds.ac.uk/
> ______________________________________________
> 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.



More information about the R-help mailing list