data.frame - Find Palindromic words in a data frame in R -
data.frame - Find Palindromic words in a data frame in R -
i trying find palindromic words in column in info frame 'data' looks like:
name year amount james 2010 934706 aza 2010 21042 rory 2010 869691 suzanne 2010 651674 felicity 2010 386115 oliver 2010 382388 anna 2010 43211
i have tried:
palindrome <- function(word) { rawword <- chartoraw(tolower(word)) ## converts lower case sprintf("%s %sa palindrome", word, c("not ", "")[identical(rawword, rev(rawword)) + 1]) } palindrome(data)
but returns list of "mary not palindrome" "anna not palindrome" ... etc want able subset words are palindromic , homecoming them info frame in order correlate them other columns find when occured , how many times.
you can next steps.
rawdata <- sapply(tolower(data$name), chartoraw) # array of booleans. true if palindromic. false if not ispalindrom <- unlist(lapply(rawdata, function(x) identical(x, rev(x)))) # palindromic words data[ispalindrom,] # non palindromic words data[! ispalindrom,]
r data.frame palindrome
Comments
Post a Comment