[R] Select varying LS digits in long numbers?
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Thu Sep 29 23:35:25 CEST 2005
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:
> Hi Folks,
>
> I'm trying to find a neat solution to an apparently simple
> problem, but one which turns out to be a bit more intricate
> and tricky than one might expect.
>
> Suppose I have numbers given to a large number of digits.
> For example
>
> 1234567021
>
> where (though I don't know this beforehand) only the last
> 3 digits will be varying (and all 3 will vary).
>
> What I want is, give a vector x of such numbers, to extract
> the minimal set of final digits which will include the varying
> digits (i.e. in this case the last 3 digits). And there may be
> a decimal point somewhere along the line (though again I won't
> know where, nor whether).
>
> I can think of brute-force ways of doing this, but I'd like
> a neat one!
>
> Best wishes to all,
> Ted.
>
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 29-Sep-05 Time: 18:45:26
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
--------------------------------------------------------------------
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 ------------------------------
More information about the R-help
mailing list