Removing digits between underscores only

Hi,
I have this dataset which has three columns,. For the first column, I want to remove the digits that are between the underscores (i.e., 1281, 1299, 1409, 1435, etc.). Note that in the first two rows, _3-_ is not part of the numbers I want to remove, just the numbers between _...._, that have no other character at all- only numbers.

 stim                            kurt       kurtval
1  abdul_mohd_1281_3-_su3uud.wav kurtosis01 131.60382
2  abdul_mohd_1299_3-_su3uud2.wavkurtosis01 151.46565
3  abdul_mohd_1409_f_faatiH.wav  kurtosis01 235.92852
4  abdul_mohd_1435_f_faatiH.wav  kurtosis01 337.57584
5  abdul_mohd_1462_T_t-aamir.wav kurtosis01  77.71517
6  abdul_mohd_1487_T_t-aamir.wav kurtosis01 214.47318
7  abdul_mohd_1514_D_d-aabil.wav kurtosis01  82.94311
8  abdul_mohd_1542_D_d-aabil.wav kurtosis01 145.74446

Here is the data.

data <- structure(list(stim = c("abdul_mohd_1281_3-_su3uud.wav", "abdul_mohd_1299_3-_su3uud2.wav", 
                             "abdul_mohd_1409_f_faatiH.wav", "abdul_mohd_1435_f_faatiH.wav", 
                             "abdul_mohd_1462_T_t-aamir.wav", "abdul_mohd_1487_T_t-aamir.wav", 
                             "abdul_mohd_1514_D_d-aabil.wav", "abdul_mohd_1542_D_d-aabil.wav"
), kurt = c("kurtosis01", "kurtosis01", "kurtosis01", "kurtosis01", 
            "kurtosis01", "kurtosis01", "kurtosis01", "kurtosis01"), kurtval = c(131.603817955143, 
                                                                                 151.465653115077, 235.928519783803, 337.575842059023, 77.7151703927855, 
                                                                                 214.473178497778, 82.9431075503998, 145.744458586239)), row.names = c(NA, 
                                                                                                                                                       8L), class = "data.frame") 

The output I am looing for is similar to this:

   stim                          kurt       kurtval
1  abdul_mohd_3-_su3uud.wav      kurtosis01 131.60382
2  abdul_mohd_3-_su3uud2.wav     kurtosis01 151.46565
3  abdul_mohd_f_faatiH.wav      kurtosis01 235.92852
4  abdul_mohd_f_faatiH.wav       kurtosis01 337.57584
5  abdul_mohd_T_t-aamir.wav      kurtosis01  77.71517
6  abdul_mohd_T_t-aamir.wav      kurtosis01 214.47318
7  abdul_mohd_D_d-aabil.wav      kurtosis01  82.94311
8  abdul_mohd_D_d-aabil.wav      kurtosis01 145.74446

I have tried the following but no success. Any thoughts?
Thank you in advance!

str_replace(data, "_\\d[0-9]_", "")

gsub('_[[:digit:]]_', '', data)

You were very close.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
data <- structure(list(stim = c("abdul_mohd_1281_3-_su3uud.wav", "abdul_mohd_1299_3-_su3uud2.wav", 
                                "abdul_mohd_1409_f_faatiH.wav", "abdul_mohd_1435_f_faatiH.wav", 
                                "abdul_mohd_1462_T_t-aamir.wav", "abdul_mohd_1487_T_t-aamir.wav", 
                                "abdul_mohd_1514_D_d-aabil.wav", "abdul_mohd_1542_D_d-aabil.wav"
), kurt = c("kurtosis01", "kurtosis01", "kurtosis01", "kurtosis01", 
            "kurtosis01", "kurtosis01", "kurtosis01", "kurtosis01"), kurtval = c(131.603817955143, 
                                                                                 151.465653115077, 235.928519783803, 337.575842059023, 77.7151703927855, 
                                                                                 214.473178497778, 82.9431075503998, 145.744458586239)), row.names = c(NA, 
                                                                                                                                                       8L), class = "data.frame") 
data2 <- data |> mutate(stim=str_replace(stim, "_\\d[0-9]+_", "_"))
data2
#>                        stim       kurt   kurtval
#> 1  abdul_mohd_3-_su3uud.wav kurtosis01 131.60382
#> 2 abdul_mohd_3-_su3uud2.wav kurtosis01 151.46565
#> 3   abdul_mohd_f_faatiH.wav kurtosis01 235.92852
#> 4   abdul_mohd_f_faatiH.wav kurtosis01 337.57584
#> 5  abdul_mohd_T_t-aamir.wav kurtosis01  77.71517
#> 6  abdul_mohd_T_t-aamir.wav kurtosis01 214.47318
#> 7  abdul_mohd_D_d-aabil.wav kurtosis01  82.94311
#> 8  abdul_mohd_D_d-aabil.wav kurtosis01 145.74446

Created on 2022-03-05 by the reprex package (v2.0.1)

1 Like

Many thanks @FJCC! it worked perfectly.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.