Reputation: 8254
I have a string in R which is something like:
File_ABC777_PatientId789.DATA
or can be something like: File_ABC7878787_234_PatientId892.DATA
I want to extract the part of the string that is in between "PatientId" and ".DATA" ...How do I do this in R ? In C# or other languages this is really easy and it's usually done using a String POS Function ... But I can't seem to find a similar function in R ...any thoughts ?
Cheers !!! MadSeb
Upvotes: 1
Views: 368
Reputation: 173627
I'm not sure what function in other languages you are specifically referring to. As sgibb mentioned, R has plenty of regex type functions.
But if your strings are always in the same format or pattern, you can simply use substr
, which simply takes a string, and the start and end positions you want to extract (or replace).
Upvotes: 2
Reputation: 25736
You could use ?sub
:
x <- "File_ABC777_PatientId789.DATA"
sub(x=x, pattern="^.*PatientId([0-9]+).DATA$", replacement="\\1")
Upvotes: 4