[R-gregmisc-commits] r2176 - in pkg/gtools: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jun 20 17:28:10 CEST 2018
Author: warnes
Date: 2018-06-20 17:28:09 +0200 (Wed, 20 Jun 2018)
New Revision: 2176
Modified:
pkg/gtools/R/baseOf.R
pkg/gtools/man/baseOf.Rd
Log:
Revert argument names for baseOf() to 'base' and 'len' instead of 'b' and 'l'.
Modified: pkg/gtools/R/baseOf.R
===================================================================
--- pkg/gtools/R/baseOf.R 2018-06-19 16:48:56 UTC (rev 2175)
+++ pkg/gtools/R/baseOf.R 2018-06-20 15:28:09 UTC (rev 2176)
@@ -1,7 +1,7 @@
-# Transform integer to array of digits in specified
+# Transform integer to array of digits in specified base
baseOf <- function(v,
- b=10,
- l=1)
+ base=10,
+ len=1)
{
if (is.null(v))
stop("v is null")
@@ -14,20 +14,20 @@
if (length(v) > 1)
{
# this returns a list which may have vectors of varying lenths
- val.list <- lapply(X=v, FUN=baseOf.inner, b=b, l=l)
+ val.list <- lapply(X=v, FUN=baseOf.inner, base=base, len=len)
longest <- max(sapply(val.list, length))
# call again, forcing all elements to have the same lenth
- retval <- t(sapply(X=v, FUN=baseOf.inner, b=b, l=longest))
+ retval <- t(sapply(X=v, FUN=baseOf.inner, base=base, len=longest))
# add informative row and column names
rownames(retval) <- paste0('v.', v)
- colnames(retval) <- paste0('b.', c(0, b^(1: (longest- 1) ) ) )
+ colnames(retval) <- paste0('b.', c(0, base^(1: (longest- 1) ) ) )
retval
}
else
- retval <- baseOf.inner(v=v, b=b, l=l)
+ retval <- baseOf.inner(v=v, base=base, len=len)
retval
}
@@ -35,22 +35,22 @@
# Transform integer to array of digits in specified
baseOf.inner <- function(v,
- b=10,
- l=1)
+ base=10,
+ len=1)
{
if (is.na(v))
- return(rep(NA, l))
+ return(rep(NA, len))
if(v==0)
- return(rep(0, l))
+ return(rep(0, len))
remainder <- v
- i <- l
+ i <- len
ret <- NULL
while(remainder > 0 || i >0)
{
#print(paste("i=",i," remainder=",remainder))
- m <- remainder%%b
+ m <- remainder%%base
if (is.null(ret))
{
ret <- m
@@ -59,12 +59,12 @@
{
ret <- c(m,ret)
}
- remainder <- remainder %/% b
+ remainder <- remainder %/% base
i <- i-1
}
if(length(ret)>1)
- names(ret) <- c(0, b^( 1:(length(ret)- 1 ) ) )
+ names(ret) <- c(0, base^( 1:(length(ret)- 1 ) ) )
return(ret)
}
Modified: pkg/gtools/man/baseOf.Rd
===================================================================
--- pkg/gtools/man/baseOf.Rd 2018-06-19 16:48:56 UTC (rev 2175)
+++ pkg/gtools/man/baseOf.Rd 2018-06-20 15:28:09 UTC (rev 2176)
@@ -11,10 +11,10 @@
\item{v}{
A single integer value to be transformed.
}
- \item{b}{
+ \item{base}{
The base to which to transform to.
}
- \item{l}{
+ \item{len}{
The minimal length of the returned array.
}
}
@@ -22,7 +22,7 @@
This function converts the elements of an integer vector 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
+ base=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.
@@ -51,32 +51,41 @@
\examples{
# decimal representation
baseOf(123)
+
# dual representation
-baseOf(123,b=2)
+baseOf(123,base=2)
+
# octal representation
-baseOf(123,b=8)
+baseOf(123,base=8)
+
# hexadecimal representation
-baseOf(123,b=16)
+baseOf(123,base=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)
+baseOf(123,len=5)
+
# and converting that back
-sum(2^(4:0)*baseOf(123,l=5))
+sum(2^(4:0)*baseOf(123,len=5))
+
# hashing and a tabular venn diagram derived from it
m<-matrix(sample(c(FALSE,TRUE),replace=TRUE,size=300),ncol=4)
colnames(m)<-c("strong","colorful","nice","humorous")
names(dimnames(m)) <- c("samples","features")
head(m)
+
m.val <- apply(m,1,function(X){return(sum(2^((ncol(m)-1):0)*X))})
m.val.rle <- rle(sort(m.val))
-m.counts <- cbind(t(baseOf(m.val.rle$value,b=2,l=ncol(m))),
- m.val.rle$length)
+m.counts <- cbind(baseOf(m.val.rle$value,base=2,len=ncol(m)),
+ m.val.rle$lengths)
colnames(m.counts)<- c(colnames(m),"num")
rownames(m.counts)<- apply(m.counts[,1:ncol(m)],1,paste,collapse="")
-m.counts[1==m.counts[,"nice"]&1==m.counts[,"humorous"],,drop=F]
+m.counts[1==m.counts[,"nice"]&1==m.counts[,"humorous"],,drop=FALSE]
m.counts[,"num",drop=T]
}
\keyword{base}
More information about the R-gregmisc-commits
mailing list