Remove characters after the second "."

Dear all,

Can someone tell me how I could remove everything after the second dot (including the second dot)?

Example:

Before: GTEX.111CU.1426.SM.5GZYP
After: GTEX.111CU

I used this before to remove after the first dot:

example$Name=gsub("\..*","",example$Name)

Thank you so much!
Bine

A more elegant approach probably exists, but here is a tidyverse way to do it.

library(tidyverse)

d = data.frame(Name = c('GTEX.111CU.1426.SM.5GZYP', 
                        'BRES.555CU.1426.SM.5GZYP')
               ) %>%
  rowwise() %>%
  mutate(Name_new = paste0(str_split(Name, '\\.')[[1]][1], 
                           '.', 
                           str_split(Name, '\\.')[[1]][2])
         ) %>%
  ungroup()

d
#> # A tibble: 2 × 2
#>   Name                     Name_new  
#>   <chr>                    <chr>     
#> 1 GTEX.111CU.1426.SM.5GZYP GTEX.111CU
#> 2 BRES.555CU.1426.SM.5GZYP BRES.555CU

Created on 2023-02-02 with reprex v2.0.2.9000

1 Like

If you prefer to keep using base R this is another option

example <- data.frame(Name = c('GTEX.111CU.1426.SM.5GZYP', 
                        'BRES.555CU.1426.SM.5GZYP')
)

example$new_Name <- regmatches(example$Name,regexpr("[a-z0-9]+\\.[a-z0-9]+", example$Name, ignore.case = TRUE))

example
#>                       Name   new_Name
#> 1 GTEX.111CU.1426.SM.5GZYP GTEX.111CU
#> 2 BRES.555CU.1426.SM.5GZYP BRES.555CU

Created on 2023-02-02 with reprex v2.0.2

2 Likes

Great, thanks a lot, guys! It worked!! :slight_smile:

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.