- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check for palindrome in R?
A palindrome is a word or any value that is being read in the same way from right to left as in left to right. For example, 12321, 514212415, ABCDEDCBA, etc. To check palindrome in R, we can create a function using stri_reverse function of stringi package as shown in the below examples.
Example1
library(stringi) palindrome<−function(x) stri_reverse(x)==x x1<−"oyo" palindrome(x1)
Output
[1] TRUE
Example2
x2<−sample(c("india","eye","ITI"),50,replace=TRUE) x2
Output
[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)
Output
[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
Example3
sample(c("redivider","defied","civic","radar","level","rotor","croma","tutor","elearne"),80,replace=TRUE) x3
Example
[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"
Output
palindrome(x3)
Output
[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
Example4
sample(c("121","124","12321","542114","5412145","3035303","212151212"),80,replace=TRUE) x4
Output
[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"
Example
palindrome(x4)
Output
[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
Advertisements