开发者

Extract non-alphabet characters from paste() function in R

The following variable:

x <- "^howdy"

Passed into the paste function like thus:

paste(x, "there", sep=".")

Returns the string "^howdy.there" as expected. 开发者_StackOverflow社区How can you eliminate the caret from howdy so the string returns only "howdy.there"?


You can do this using gsub:

paste(gsub("^","",x,fixed=TRUE),"there",sep=".")


If you have a character vector and need to remove any non-alphanumeric characters, this slightly-more-complicated regular expression will be more efficient than explicitly specifying each character manually.

> gsub("[^[:alnum:]._]","",c("&hi_there%","^howdy.there"))
[1] "hi_there"    "howdy.there"

In regular expressions, the contents of [] are called a "character class" and each character inside the [] will be matched (or not matched if the first character is ^ as in the example above). So we can use gsub to replace all characters that aren't alphanumeric, period, or underscore with the null string "".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜