Andrej Kveder
2003-Oct-08 19:39 UTC
[R] 2 questions regarding base-n and identifing digits
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? (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 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]]
> 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 4Would 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 >
Gabor Grothendieck
2003-Oct-09 00:20 UTC
[R] 2 questions regarding base-n and identifing digits
Check out: http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/decodeencode/decodeencode.rev --- Date: Wed, 8 Oct 2003 21:39:50 +0200 From: Andrej Kveder <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? (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 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 _______________________________________________ No banners. No pop-ups. No kidding. Introducing My Way - http://www.myway.com
Andrej Kveder
2003-Oct-09 08:09 UTC
[R] 2 questions regarding base-n and identifing digits + the source problem description
Thanks for the suggestions.. The strsplit function works great for the second question... I will have to come up with another solution to the first one, since my problem would definetly involve a lot of calculations and I think that current methods would cause major computational congestion... BUt here is the outline of my problem... if anybody has an idea... In practice I start with 4 possible correlations: -0.9, -0.1, 0.1, 0.9 and want to construct all possible 3x3 correaltion matrices with those values. So I need to do all possible permutations of a given length with replication. I have a set of 4 distinct values and want to create the permutations of length 3. My idea was as folows: specifing the sequence in base-4 form 0 to 333 would account for all possible combinations. I checked some of the existing functions which were either recursive and slow or without replication. I think there was a thread on the list just a short while ago. If anybody has a hint to follow, i would be more then glad to try it out... Thanks again 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