开发者

Remove everything before period [duplicate]

开发者 https://www.devze.com 2023-03-30 19:51 出处:网络
This question already has answers here: How do I specify a dynamic position for the start of substring?
This question already has answers here: How do I specify a dynamic position for the start of substring? (4 answers) Closed 2 years ago.

I want to remove everything before period (.) sign in the following stri开发者_运维问答ng in R. I tried with gsub function.

Test <- c("Data.A", "Data.B", "Data.C", "Data.D")
gsub("[.]", "", Test)

Any help will be highly appreciated. Thanks


Try this: gsub("^.*\\.", "", Test)

What's it doing? Matches the beginning of the string with any character, any number of times. Then matches a single period. It replaces all of that with nothing.

> gsub("^.*\\.", "", Test)
[1] "A" "B" "C" "D"


Or if you find regular expressions abhorrent, you could use sapply and strsplit:

sapply(strsplit(Test,".",fixed = TRUE),"[[",2)
[1] "A" "B" "C" "D"

This is splitting each element on the '.' and then grabbing just the second element of the result from each.

0

精彩评论

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