split character data into numbers and letters

后端 未结 7 1726
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 15:45

I have a vector of character data. Most of the elements in the vector consist of one or more letters followed by one or more numbers. I wish to split each element in the

相关标签:
7条回答
  • 2020-12-02 16:46

    A slightly more elegant way (without any external packages):

    > x = c("aaa", "b11", "b21", "b101", "b111", "ccc1", "ffffd1", "ccc20", "ffffd13")
    > gsub('\\D','', x)       # replaces non-digits with blancs
    [1] ""    "11"  "21"  "101" "111" "1"   "1"   "20"  "13" 
    > gsub('\\d','', x)       # replaces digits with blanks
    [1] "aaa" "b"   "b"   "b"   "b"   "ccc" "ffffd" "ccc" "ffffd"
    
    0 讨论(0)
提交回复
热议问题