How to Reverse a string in R

后端 未结 14 2057
花落未央
花落未央 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: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.

提交回复
热议问题