Search code examples
rplottextlabelelement

Select a single element from a complex ID


I have IDs such as "3_K97_T12_High_2_Apples". I want to select just "T12" and store it in a character vector (Tiles) so I can call it in text() when I label my points in the plot() with label = Tiles. I want to label each point with just the 3rd element of the ID (i.e T12).

How can I do this?


Solution

  • Use a regex to extract - capture the third set of characters that are not a _ (([^_]+) from the start (^) of the string as a group and in the replacement specify the backreference (\\1) of the captured group

    Tiles <- sub("^[^_]+_[^_]+_([^_]+)_.*", "\\1", str1)
    Tiles
    [1] "T12"
    

    ^ - start of the string

    [^_]+ - one or more characters not a _

    _ - the _

    [^_]+ - one or more characters not a _

    _ - the _

    ([^_]+) - one or more characters not a _ captured

    data

    str1 <- "3_K97_T12_High_2_Apples"