[R] Code to fetch summary info from vector

Rui Barradas ruipbarradas at sapo.pt
Tue Jan 15 18:34:45 CET 2013


Hello,

Continuing Jessica's code, to get the maximum of each group just use

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)
groups <- rep(1:length(r$lengths),r$lengths)

tapply(b, groups, FUN = max)

# To get just the groups where b > 1,

mx <- tapply(b, groups, FUN = max)
mx[mx > 1]

# And you can combine both like in

cbind(length = r$lengths[r$values], max = mx[mx > 1])


Hope this helps,

Rui Barradas
Em 15-01-2013 16:57, Jessica Streicher escreveu:
> Maybe rle can help a little here
>
> rle(b>1)
>
> Run Length Encoding
>    lengths: int [1:5] 3 5 5 8 3
>    values : logi [1:5] FALSE TRUE FALSE TRUE FALSE
>
>   r<-rle(b>1)
>
> r$lengths[r$values]
> [1] 5 8
>
> # started for the maximum but need to go home now, sorry. Will continue tomorrow if noone else finishes it.
> groups <- rep(1:length(r$lengths),r$lengths)
>
> On 15.01.2013, at 17:16, Benjamin Gillespie wrote:
>
>> 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.
> ______________________________________________
> 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