0

I have a list

t <- list('mcd.norm_1','mcc.norm_1', 'mcr.norm_1')

How can i convert the list to remove the period and everything after so the list is just

'mcd' 'mcc' 'mcr'
camille
  • 16,432
  • 18
  • 38
  • 60
Eisen
  • 1,697
  • 9
  • 27

3 Answers3

0

You may try

library(stringr)

lapply(t, function(x) str_split(x, "\\.", simplify = T)[1])
Park
  • 14,771
  • 6
  • 10
  • 29
0

Another possible solution:

library(tidyverse)

t <- list('mcd.norm_1','mcc.norm_1', 'mcr.norm_1')

t %>% 
  str_remove("\\..*")

#> [1] "mcd" "mcc" "mcr"
PaulS
  • 21,159
  • 2
  • 9
  • 26
0

This could be another option:

unlist(sapply(t, \(x) regmatches(x, regexec(".*(?=\\.)", x, perl = TRUE))))

[1] "mcd" "mcc" "mcr"
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41