Reverse digits in R

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

    I don't think rev quite does it. It reverses the elements of the vector, while the question is how to reverse the elements in the vector.

    > nums <- sapply(1:10,function(i)as.numeric(paste(sample(1:9,6,TRUE),collapse="")))
    > nums
     [1] 912516 568934 693275 835117 155656 378192 343266 685182 298574 666354
    > sapply(strsplit(as.character(nums),""), function(i) paste(rev(i),collapse=""))
     [1] "615219" "439865" "572396" "711538" "656551" "291873" "662343" "281586" "475892" "453666"
    

提交回复
热议问题