[R] Finding sets

David Winsemius dwinsemius at comcast.net
Fri Jun 25 13:47:19 CEST 2010


On Jun 25, 2010, at 5:43 AM, Dennis Murphy wrote:

> Hi:
>
> Here's one approach:
>
>> x <- rep(c(1,2,1,3,5), each=5)
>> rle(x)
> Run Length Encoding
>  lengths: int [1:5] 5 5 5 5 5
>  values : num [1:5] 1 2 1 3 5
>> table(rle(x)$values)
>
> 1 2 3 5
> 2 1 1 1
>> unname(table(rle(x)$values))[1]
> [1] 2
>

This method does not require visual inspection of the intermediate  
result:

 > sum(rle(x)$values==1)
[1] 2

-- 
David.

> HTH,
> Dennis
>
> On Fri, Jun 25, 2010 at 2:30 AM, Muhammad Rahiz <
> muhammad.rahiz at ouce.ox.ac.uk> wrote:
>
>> Hi all,
>>
>> I'd like to find how many sets of 1s there are in the following  
>> example;
>>
>> x <- rep(c(1,2,1,3,5), each=5)
>>
>> I know that there are two sets of 1s, visually. Any function in R  
>> that
>> allows me to automate the process?
>>
>> Thanks.
>>
>>
>> Muhammad
>>
>> ______________________________________________
>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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