Reputation:
I have a string of names in the following format:
names <- c("Q-1234-1", "Q-1234-2", "Q-1234-1-8", "Q-1234-2-8")
I am trying to extract the single digit after the second hyphen. There are instances where there will be a third hyphen and an additional digit at the end of the name. The desired output is:
1, 2, 1, 2
I assume that I will need to use sub/gsub but am not sure where to start. Any suggestions?
Upvotes: 1
Views: 65
Reputation: 1080
Here's an alternative using stringr
library("stringr")
names <- c("Q-1234-1", "Q-1234-2", "Q-1234-1-8", "Q-1234-2-8")
output = str_split_fixed(names, pattern = "-", n = 4)[,3]
Upvotes: 0
Reputation: 886938
We can use sub
to match the pattern of zero or more characters that are not a -
([^-]*
) from the start (^
) of the string followed by a -
followed by zero or more characters that are not a -
followed by a -
and the number that follows being captured as a group. In the replacement, we use the backreference of the captured group (\\1
)
as.integer(sub("^[^-]*-[^-]*-(\\d).*", "\\1", names))
#[1] 1 2 1 2
Or this can be modified to
as.integer(sub("^([^-]*-){2}(\\d).*", "\\2", names))
#[1] 1 2 1 2
Upvotes: 1