Search code examples
rstringsplittailseparator

String remove from n-th last seperator to the end


I have the following string:

data_string = c("Aa_Bbbbb_0_ID1",
                "Aa_Bbbbb_0_ID2",
                "Aa_Bbbbb_0_ID3",
                "Ccccc_D_EEE_0_ID1")

I just wanted to split all the string to have these results:

"Aa_Bbbbb"
"Aa_Bbbbb"
"Aa_Bbbbb"
"Ccccc_D_EEE"

So basically, I'm looking for a function which take data_string, set a separator, and take the split position :

remove_tail(data_table, sep = '_', del = 2)

only removing the tail from 2nd last separator to the end of the string (not split all the string)


Solution

  • Try below:

    # split on "_" then paste back removing last 2
    sapply(strsplit(data_string, "_", fixed = TRUE),
           function(i) paste(head(i, -2), collapse = "_"))
    

    We can make our own function:

    # custom function
    remove_tail <- function(x, sep = "_", del = 2){
      sapply(strsplit(x, split = sep, fixed = TRUE),
             function(i) paste(head(i, -del), collapse = sep))
      }
    
    remove_tail(data_string, sep = '_', del = 2)
    # [1] "Aa_Bbbbb"    "Aa_Bbbbb"    "Aa_Bbbbb"    "Ccccc_D_EEE"