Skip to content
Prev 78117 / 398502 Next

Select varying LS digits in long numbers?

I've received what looks like a good -- and neat! -- solution
off-list from Hadley Wickham. Suppose "numbers" is a vector of
long integers. Then Hadley wrote:
digits <- outer(numbers, 10:0,
                  function(x,y) numbers %/% 10^y %% 10)

  apply(digits,2, function(x) length(unique(x)))

This certainly looks cast-iron, and nicely passes my "slipperiness"
test:

  numbers<-c(1234566999,1234567001)
  digits <- outer(numbers, 10:0, function(x,y) numbers %/% 10^y %% 10)
  result<-apply(digits,2, function(x) length(unique(x))); result
# [1] 1 1 1 1 1 1 1 2 2 2 2

  d1<-min(which(result>1)); d1
# [1] 8

  numbers%%(10^(12-d1))
# [1] 6999 7001

as desired!

Thanks also to Patrick Burns and Jim Holtman for other suggestions
based (in effect) on diff(range(numbers)).
On 29-Sep-05 Ted Harding wrote:
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 29-Sep-05                                       Time: 22:35:15
------------------------------ XFMail ------------------------------