Search code examples
rextract

How to extract elements of variable names using grep in R


I have variable names in the form A_B_C_D. I would like to extract anything that appears after the second _. So C and D, leaving out the underscore.

Can someone please help me do this?

Thank you.


Solution

  • You can try

    unlist(strsplit("A_B_C_D", "_"))[-(1:2)]
    
    [1] "C" "D"