Reputation: 7735
I am importing a file with column names that take the following format:
x <- "text.text.text.text.name"
In most cases, the following line is good for removing everything before the final period, leaving only the variable name I want (name
):
gsub(".*\\.","", x)
However, some variables have the following format:
y <- "text.text.text.text.name.R1"
I'd like to keep name.R1
when the final period is followed by a capital "R". How would I modify the gsub
argument to permit this change?
Upvotes: 0
Views: 263
Reputation: 7735
@kasterma's solution worked best for me:
gsub("(.*\\.)([^R])","\\2", x)
Upvotes: 0
Reputation: 215117
You can use a negative look ahead to constrain the final period:
gsub(".*\\.(?!R)", "", x, perl = T)
# [1] "name"
gsub(".*\\.(?!R)", "", y, perl = T)
# [1] "name.R1"
Upvotes: 1