How to Reverse a string in R

后端 未结 14 1949
花落未央
花落未央 2020-11-27 03:29

I\'m trying to teach myself R and in doing some sample problems I came across the need to reverse a string.

Here\'s what I\'ve tried so far but the paste operation d

相关标签:
14条回答
  • 2020-11-27 04:12

    If your data is in a data.frame, you can use sqldf:

    myStrings <- data.frame(forward = c("does", "this", "actually", "work"))
    library(sqldf)
    sqldf("select forward, reverse(forward) `reverse` from myStrings")
    #    forward  reverse
    # 1     does     seod
    # 2     this     siht
    # 3 actually yllautca
    # 4     work     krow
    
    0 讨论(0)
  • 2020-11-27 04:12

    The following Code will take input from user and reverse the entire string-

    revstring=function(s)
    print(paste(rev(strsplit(s,"")[[1]]),collapse=""))
    
    str=readline("Enter the string:")
    revstring(str)
    
    0 讨论(0)
提交回复
热议问题