回文是从右到左以与从左到右相同的方式读取的单词或任何值。例如12321、514212415,ABCDEDCBA等。要检查R中的回文,我们可以使用stringi包的stri_reverse函数创建一个函数,如以下示例所示。
library(stringi) palindrome<−function(x) stri_reverse(x)==x x1<−"oyo" palindrome(x1)输出结果
[1] TRUE
x2<−sample(c("india","eye","ITI"),50,replace=TRUE) x2输出结果
[1] "india" "india" "ITI" "ITI" "ITI" "ITI" "ITI" "india" "india" [10] "eye" "eye" "ITI" "india" "eye" "eye" "eye" "eye" "ITI" [19] "eye" "ITI" "eye" "ITI" "ITI" "ITI" "ITI" "eye" "eye" [28] "ITI" "eye" "eye" "ITI" "eye" "ITI" "india" "eye" "eye" [37] "eye" "eye" "eye" "ITI" "eye" "india" "india" "ITI" "eye" [46] "india" "eye" "ITI" "india" "eye"
palindrome(x2)输出结果
[1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE [13] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [25] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE [37] TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE TRUE [49] FALSE TRUE
sample(c("redivider","defied","civic","radar","level","rotor","croma","tutor","elearne"),80,replace=TRUE) x3
[1] "radar" "defied" "croma" "tutor" "radar" "croma" [7] "croma" "tutor" "radar" "civic" "elearne" "tutor" [13] "civic" "croma" "radar" "elearne" "tutor" "tutor" [19] "radar" "rotor" "civic" "level" "defied" "elearne" [25] "defied" "croma" "tutor" "radar" "croma" "level" [31] "elearne" "civic" "croma" "redivider" "tutor" "tutor" [37] "redivider" "tutor" "tutor" "radar" "rotor" "croma" [43] "defied" "radar" "tutor" "rotor" "civic" "redivider" [49] "radar" "tutor" "redivider" "redivider" "redivider" "defied" [55] "level" "redivider" "radar" "civic" "rotor" "rotor" [61] "elearne" "radar" "redivider" "croma" "rotor" "defied" [67] "redivider" "elearne" "radar" "rotor" "radar" "civic" [73] "radar" "radar" "defied" "radar" "radar" "redivider" [79] "redivider" "civic"输出结果
palindrome(x3)输出结果
[1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE [13] TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE [25] FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE [37] TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE [49] TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE [61] FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE [73] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
sample(c("121","124","12321","542114","5412145","3035303","212151212"),80,replace=TRUE) x4输出结果
[1] "124" "542114" "212151212" "12321" "212151212" "5412145" [7] "12321" "212151212" "542114" "212151212" "212151212" "5412145" [13] "12321" "124" "3035303" "121" "121" "121" [19] "3035303" "12321" "5412145" "12321" "212151212" "212151212" [25] "212151212" "3035303" "121" "3035303" "124" "3035303" [31] "212151212" "542114" "212151212" "542114" "212151212" "542114" [37] "3035303" "5412145" "12321" "121" "542114" "3035303" [43] "542114" "124" "124" "212151212" "12321" "3035303" [49] "3035303" "3035303" "542114" "121" "3035303" "124" [55] "212151212" "542114" "3035303" "121" "212151212" "3035303" [61] "121" "542114" "5412145" "212151212" "3035303" "5412145" [67] "3035303" "124" "5412145" "542114" "5412145" "12321" [73] "124" "3035303" "124" "212151212" "542114" "542114" [79] "3035303" "12321"
palindrome(x4)输出结果
[1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE [13] TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [25] TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE [37] TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE TRUE [49] TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE [61] TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE [73] FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE