Hello, Splus contains the function intbin(x,l). This function allows to make a conversion from an integer x to a binary of length l. for example intbin(3,2) returns 11 intbin(3,3) returns 011 Do you know how to do it in R ? Thank you meriema
(no subject)
3 messages · meriema.aupetit@free.fr, Peter Dalgaard, Peter Wolf
meriema.aupetit at free.fr writes:
Hello, Splus contains the function intbin(x,l). This function allows to make a conversion from an integer x to a binary of length l. for example intbin(3,2) returns 11 intbin(3,3) returns 011 Do you know how to do it in R ?
How about this?
intbin <- function(x, n)
if(n)
paste(intbin(x%/%2, n-1), x%%2, sep="")
else if (x)
stop("Insufficient field width")
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
meriema.aupetit at free.fr wrote:
Hello, Splus contains the function intbin(x,l). This function allows to make a conversion from an integer x to a binary of length l. for example intbin(3,2) returns 11 intbin(3,3) returns 011 Do you know how to do it in R ? Thank you meriema
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
you can use the following function
encode <- function(number, base) {
# simple version of APL-encode / APL-representation "T", pw 10/02
# "encode" converts the numbers "number" using the radix vector "base"
n.base <- length(base); result <- matrix(0, length(base), length(number))
for(i in n.base:1){
result[i,] <- if(base[i]>0) number %% base[i] else number
number <- ifelse(rep(base[i]>0,length(number)),
floor(number/base[i]), 0)
}
return( if(length(number)==1) result[,1] else result )
}
paste(encode(c(13), c(2,2, 2, 2, 2)),collapse="")
[1] "01101"
encode(c(13), c(2,2, 2, 2, 2))
[1] 0 1 1 0 1
See also:
http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/decodeencode/decodeencode.rev
Peter