Reverse digits in R

后端 未结 7 2040
清歌不尽
清歌不尽 2020-12-15 11:48

How can you reverse a string of numbers in R?

for instance, I have a vector of about 1000 six digit numbers, and I would like to know if they are palindromes. I wo

相关标签:
7条回答
  • 2020-12-15 12:21

    This should work in the general case, with any choice of base:

    is.palindromic <- function(x, base=10)
    {
        p <- 0
        m <- floor(log(x,base))
        sig <- -1
        for (i in m:0)
            {
            tp <- floor(x/base^i)
            a <- i+1
            b <- m+1-i
            if(a==b){c<-0}else{c<-a*b;sig<-sig*-1}
            p <- p + tp*c*sig
            x <- x - tp*base^i
            }
        return(!as.logical(p))
    }
    
    0 讨论(0)
提交回复
热议问题