How to Reverse a string in R

后端 未结 14 1948
花落未央
花落未央 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 03:46

    Here is one more base-R solution:

    # Define function
    strrev <- function(x) {
      nc <- nchar(x)
      paste(substring(x, nc:1, nc:1), collapse = "")
    }
    
    # Example
    strrev("Sore was I ere I saw Eros")
    [1] "sorE was I ere I saw eroS"
    

    Solution was inspired by these U. Auckland slides.

    0 讨论(0)
  • 2020-11-27 03:47

    So apparently front-end JS developers get asked to do this (for interviews) in JS without using built-in reverse functions. It took me a few minutes, but I came up with:

    string <- 'hello'
    
    foo <- vector()
    
    for (i in nchar(string):1) foo <- append(foo,unlist(strsplit(string,''))[i])
    paste0(foo,collapse='')
    

    Which all could be wrapped in a function...

    What about higher-order functionals? Reduce?

    0 讨论(0)
  • 2020-11-27 03:54

    The following can be a useful way to reverse a vector of strings x, and is slightly faster (and more memory efficient) because it avoids generating a list (as in using strsplit):

    x <- rep( paste( collapse="", LETTERS ), 100 )
    str_rev <- function(x) {
      sapply( x, function(xx) { 
        intToUtf8( rev( utf8ToInt( xx ) ) )
      } )
    }
    str_rev(x)
    

    If you know that you're going to be working with ASCII characters and speed matters, there is a fast C implementation for reversing a vector of strings built into Kmisc:

    install.packages("Kmisc")
    str_rev(x)
    
    0 讨论(0)
  • 2020-11-27 03:55

    As @mplourde points out, you want the collapse argument:

    paste(test_rev, collapse='')
    

    Most commands in R are vectorized, but how exactly the command handles vectors depends on the command. paste will operate over multiple vectors, combining the ith element of each:

    > paste(letters[1:5],letters[1:5])
    [1] "a a" "b b" "c c" "d d" "e e"
    

    collapse tells it to operate within a vector instead.

    0 讨论(0)
  • 2020-11-27 03:55

    From ?strsplit, a function that'll reverse every string in a vector of strings:

    ## a useful function: rev() for strings
    strReverse <- function(x)
            sapply(lapply(strsplit(x, NULL), rev), paste, collapse="")
    strReverse(c("abc", "Statistics"))
    # [1] "cba"        "scitsitatS"
    
    0 讨论(0)
  • 2020-11-27 03:57

    Here is a function that returns the whole reversed string, or optionally the reverse string keeping only the elements specified by index, counting backward from the last character.

    revString = function(string, index = 1:nchar(string)){
      paste(rev(unlist(strsplit(string, NULL)))[index], collapse = "")
    }
    

    First, define an easily recognizable string as an example:

    (myString <- paste(letters, collapse = ""))

    [1] "abcdefghijklmnopqrstuvwxyz"

    Now try out the function revString with and without the index:

    revString(myString)

    [1] "zyxwvutsrqponmlkjihgfedcba"

    revString(myString, 1:5)

    [1] "zyxwv"

    0 讨论(0)
提交回复
热议问题