[R] Gap in solution

David Winsemius dwinsemius at comcast.net
Fri Oct 28 17:38:57 CEST 2011


On Oct 28, 2011, at 4:08 AM, Samir Benzerfa wrote:

> Hi David,
>
>
>
> In the general case, there is still a gap in your solution  
> >sum( tbl["1",
> 2:ncol(tbl)] ). This solution refers to a specific column number  
> (here:
> column number 2) and not to the actual length of the run, doesn't it?

That is correct. Fully tested solutions are provided when complete  
example code displaying the complexity of the problem is offered. If I  
had been posed a question that actually had a "gap" then the first  
thing I would have tried would be to construct a logical test for the  
condition under scrutiny and apply it to the indexing system.  Using  
you example below (which is deficint in not having more than one  
column with hits)

 > str(with(runs, table(values, lengths)))
  'table' int [1:7, 1:2] 3 1 1 2 2 1 2 1 0 0 ...
  - attr(*, "dimnames")=List of 2
   ..$ values : chr [1:7] "1" "2" "3" "4" ...
   ..$ lengths: chr [1:2] "1" "3"
 > xtbl <- with(runs, table(values, lengths))
 > attr(xtbl, "dimnames")$lengths
[1] "1" "3"
 > which( as.numeric( attr(xtbl, "dimnames")$lengths) >=2 )
[1] 2

You could have just used that value, but I think it would be useful to  
test it with an example that has another run length > 2 including one  
that is "disjpoint"

 > x=c(1,3,4,9,1,9,1,5,4,5,2,1,1,1,6, 1,1,1,1,1)
 > xtbl <- with(rle(x), table(values, lengths))
 > xtbl
       lengths
values 1 3 5
      1 3 1 1
      2 1 0 0
      3 1 0 0
      4 2 0 0
      5 2 0 0
      6 1 0 0
      9 2 0 0
 > which( as.numeric( attr(xtbl, "dimnames")$lengths) >=2 )
[1] 2 3

So this is a better tested solution:
 > xtbl["1", which( as.numeric( attr(xtbl, "dimnames")$lengths) >=2 )]
3 5
1 1

-- 
David.

> That
> is, in this simple example the column number 2 actually corresponds  
> to the
> length "2", but this must not be the case in general. For instance  
> if there
> is no run of length "2" but only of length "1" and "3", the column  
> number 2
> will refer to length "3" (try it with the new vector below). I  
> realized this
> problem when applying your solution to a much more extended vector.  
> So, the
> problem is that I would have to check manually whether the column  
> number
> really corresponds to the length of runs. A possible solution would  
> be to
> force R to show all the lengths from 1:ncol even if there is no run  
> of some
> lengths in-between and just fill the whole column with zero's.
>
>
>
>> x=c(1,3,4,9,1,9,1,5,4,5,2,1,1,1,6)
>
>
>
> Any ideas how to solve this problem?
>
>
>
> Cheers, S.B.
>
>
>
>
>
> -----Ursprüngliche Nachricht-----
>
> Von: David Winsemius [mailto:dwinsemius at comcast.net]
>
> Gesendet: Donnerstag, 27. Oktober 2011 16:44
>
> An: Duncan Murdoch
>
> Cc: Samir Benzerfa; r-help at r-project.org
>
> Betreff: Re: [R] question R regarding consecutive numbers
>
>
>
>
>
> On Oct 27, 2011, at 9:21 AM, Duncan Murdoch wrote:
>
>
>
>> On 27/10/2011 8:43 AM, Samir Benzerfa wrote:
>
>>> Hi  everyone
>
>>>
>
>>>
>
>>>
>
>>> Do you know about any possibility in R to check for consecutive
>
>>> numbers in
>
>>> vectors? That is, I do not only want to count the number of
>
>>> observations in
>
>>> total (which can be done table(x)), but I also want to count for
>
>>> instance
>
>>> how many times that vector contains a certain number consecutively.
>
>>>
>
>>>
>
>>>
>
>>> For example in the following vector x the number "1" appears 7  
>>> times.
>
>>> However, I want to check for instance how many times two
>
>>> consecutive 1's
>
>>> appear in the vector, which would actually be two times the case in
>
>>> the
>
>>> below vector.
>
>>>
>
>>>
>
>>>
>
>>>> x=c(1,1,3,4,9,1,9,1,5,4,5,2,1,1,1,6)
>
>>>
>
>>>
>
>>>
>
>>> Any ideas for this issue?
>
>>
>
>> How about this?
>
>>
>
>>> runs <- rle(x)
>
>>> with(runs, table(values, lengths))
>
>
>
> And to go even a bit further, the table function returns a matrix
>
> which can be addressed to yield the specific answer requested:
>
>
>
>  with(runs, table(values, lengths))["1",2]
>
> [1] 1  # m=number of exactly runs if length 2
>
>> sum( tbl["1", 2:ncol(tbl)] )
>
> [1] 2  # number of runs of length two or more.
>
>
>
>
>
> -- 
>
> David
>
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list