Reverse digits in R

后端 未结 7 2046
清歌不尽
清歌不尽 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:17

    There is function in stringi package for that - stri_reverse

    require(stringi)
    stri_reverse("123456")
    ## [1] "654321"
    

    Now palindrome function might as simple as that:

    palindrome <- function(x) stri_reverse(x)==x
    palindrome(c("651156","1234321"))
    ## [1] TRUE  TRUE
    

提交回复
热议问题