cb2 <- function(num){
td<-{}
a <- {}
while (num 2 > 0 ){
a <- num %% 2
td <- paste(td,a, sep="")
num <- as.integer(num / 2)
}
return(td)
}
用法是:
sapply(1:10, cb2)
我想概括这个函数,并包括首选基(s)作为参数的函数,阿拉。。。
convertbase <- function(num, base){
td<-{}
a <- {}
while (num / base > 0 ){
a <- num %% base
td <- paste(td,a, sep="")
num <- as.integer(num / base)
}
return(td)
}
如果我只对一个以2-10为基数的数字感兴趣,那就好了:
mapply(convertbase, 10, 2:10)
mapply(convertbase, 1:10, 2:10)
Warning message:
In mapply(convertbase, 1:10, 2:10) :
longer argument not a multiple of length of shorter
理想情况下,这个函数或函数集将返回一个数据帧,其中以2-10为底有单独的列,但是我意识到在我的代码和目标之间缺少一些东西。任何帮助都将不胜感激。