[R] 2 questions regarding base-n and identifing digits

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Oct 9 01:09:16 CEST 2003


On 08-Oct-03 Liaw, Andy wrote:
>> From: Andrej Kveder [mailto:andrejk at zrc-sazu.si] 
>> I have two questions:
>> (1)
>> Is there a way in R to change the base-n of the calculations. 
>> I wnat to run some calculations either in binary (base-2) or 
>> base-4. Is there a way to specify that in R - to chnage from 
>> the decimal?
> 
> I don't have any good answers, but just this:  Arithmetics in any base
> is the same.  I believe you just need a way to convert decimal to other
> bases and printed out as such.  E.g., 1 + 5 = 6 = 110 in binary.  You
> just need something that returns 110 when given 6.  Personally I'd
> write such a thing in C because I don't know any better.

Indeed! A pity that R's sprintf (though described as a wrapper for C's
sprintf) does not accept the C format conversion specifiers "%X" (for
hexadecimal) or, better stil, "%o" (for octal), because then Andy's
strsplit usage below could give you one of 16 or 8 characters which could
be used to index a lookup of a bit pattern!

Ted.

> Would the following do what you want?
> 
>> a <- c(1234, 5678, 90)
>> foo <- function(x) strsplit(as.character(x), "")
>> foo(a)
> [[1]]
> [1] "1" "2" "3" "4"
> 
> [[2]]
> [1] "5" "6" "7" "8"
> 
> [[3]]
> [1] "9" "0"

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 09-Oct-03                                       Time: 00:09:16
------------------------------ XFMail ------------------------------




More information about the R-help mailing list