[R] Select varying LS digits in long numbers?

Paul Johnson pauljohn at ku.edu
Tue Oct 4 19:10:51 CEST 2005


Maybe this is just the brute force you want to avoid, but it works to 
coerce the integer into a string, and then back to a number.  Could 
reduce number of lines by nesting functions, of course.

y <- 1234131431
n <- 3
ychar <- as.character(y)
ydigits <- nchar(ychar)
result <- as.numeric ( substr(ychar, ydigits- n +1, ydigits) )

pj

(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).
> 



-- 
Paul E. Johnson                       email: pauljohn at ku.edu
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700




More information about the R-help mailing list