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

Liaw, Andy andy_liaw at merck.com
Wed Oct 8 22:53:21 CEST 2003


> From: Andrej Kveder [mailto:andrejk at zrc-sazu.si] 
> 
> Dear listers,
> 
> 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.
 
> (2)
> I also want to extract the digits from a larger number and 
> store them as a vector. I could do it through converting top 
> string, parsing the string and converting back. Is there 
> another way. It would look something like that:
> > a<-1234
> > a
> [1] 1234
> > b<-foo(a)
> > b
>      [,1] [,2] [,3] [,4]
> [1,]    1    2    3    4

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"

HTH,
Andy
 
> Thanks!
> 
> Andrej
> 
> _________
> Andrej Kveder, M.A.
> researcher
> Institute of Medical Sciences SRS SASA; Novi trg 2, SI-1000 
> Ljubljana, Slovenia
> phone: +386 1 47 06 440   fax: +386 1 42 61 493
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
>




More information about the R-help mailing list