R: removing numbers at begin and end of a string

后端 未结 3 1785
小鲜肉
小鲜肉 2020-12-19 09:56

I\'ve got the following vector:

words <- c(\"5lang\",\"kasverschil2\",\"b2b\")

I want to remove \"5\" in \"5lang\"

3条回答
  •  有刺的猬
    2020-12-19 10:41

    Get instances where numbers appear at the beginning or end of a word and match everything else. You need to collapse results because of possible multiple matches:

    gregexpr(pattern='(^[0-9]+|[0-9]+$)', text = words) -> mm
    sapply(regmatches(words, mm, invert=TRUE), paste, collapse="") 
    

提交回复
热议问题