[R-gregmisc-commits] r2172 - in pkg/gtools: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Aug 24 01:03:37 CEST 2017
Author: warnes
Date: 2017-08-24 01:03:36 +0200 (Thu, 24 Aug 2017)
New Revision: 2172
Added:
pkg/gtools/R/baseOf.R
pkg/gtools/man/baseOf.Rd
Log:
Add `baseOf` function and documentation to support updated `gplots::venn` function. (Provided by Steffen M?\195?\182ller.)
Added: pkg/gtools/R/baseOf.R
===================================================================
--- pkg/gtools/R/baseOf.R (rev 0)
+++ pkg/gtools/R/baseOf.R 2017-08-23 23:03:36 UTC (rev 2172)
@@ -0,0 +1,32 @@
+
+# transform base
+#
+# This function rewrites regular integer numbers as an array of its digits.
+# The base of the numbering scheme may be changed away from 10,
+# which defines our decimal system, to any other integer value. For
+# b=2, the number is returned in the dual system. The least significant
+# digit has the highest index in the array, i.e. it appears on the right.
+#
+# v = value of base 10 to be transformed
+# b = new base
+# l = minimal length of returned array (default is 1)
+# return value: array of factors, highest exponent first
+baseOf<-function(v,b=10,l=1) {
+ remainder<-v
+ i<-l
+ ret<-NULL
+ while(remainder>0 || i>0) {
+ #print(paste("i=",i," remainder=",remainder))
+ m<-remainder%%b
+ if (is.null(ret)) {
+ ret<-m
+ }
+ else {
+ ret<-c(m,ret)
+ }
+ remainder <- remainder %/% b
+ i<-i-1
+ }
+ return(ret)
+}
+
Added: pkg/gtools/man/baseOf.Rd
===================================================================
--- pkg/gtools/man/baseOf.Rd (rev 0)
+++ pkg/gtools/man/baseOf.Rd 2017-08-23 23:03:36 UTC (rev 2172)
@@ -0,0 +1,47 @@
+\name{baseOf}
+\alias{baseOf}
+\title{Transform integer to new base}
+\description{
+Transforms a given base-10 integer to an array of digits of another base.
+}
+\usage{
+baseOf(v,b=10,l=1)
+}
+\arguments{
+ \item{v}{
+ A single integer value to be transformed.
+ }
+ \item{b}{
+ The base to which to transform to.
+ }
+ \item{l}{
+ The minimal length of the returned array.
+ }
+}
+\details{
+ This function rewrites regular integer numbers as an array of its digits.
+ The base of the numbering scheme may be changed away from 10,
+ which defines our decimal system, to any other integer value. For
+ b=2, the number is returned in the dual system. The least significant
+ digit has the highest index in the array, i.e. it appears on the right.
+ The highest exponent is at position 1, i.e. left.
+}
+\author{Steffen Moeller \email{moeller at debian.org} }
+\examples{
+# decimal representation
+baseOf(123)
+# dual representation
+baseOf(123,b=2)
+# octal representation
+baseOf(123,b=8)
+# hexadecimal representation
+baseOf(123,b=16)
+# hexadecimal with more typical letter-notation
+c(0:9,LETTERS)[baseOf(123,16)]
+# hexadecimal again, now showing a single string
+paste(c(0:9,LETTERS)[baseOf(123,16)],collapse="")
+# decimal representation but filling leading zeroes
+baseOf(123,l=5)
+}
+\keyword{base}
+
More information about the R-gregmisc-commits
mailing list