[R] Best way to test for numeric digits?

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Wed Oct 18 21:26:20 CEST 2023


The matching approach is also competitive:

match.symbol2 <- function(x, rm.digits = TRUE) {
 if (rm.digits) stringi::stri_extract_all(x, regex = '[A-Z][a-z]*') else
 lapply(
  stringi::stri_match_all(x, regex = '([A-Z][a-z]*)([0-9]*)'), \(m) {
   m <- t(m[,2:3]); m[nzchar(m)]
  }
 )
}
mol50000 <- rep(mol, 50000)
system.time(split.symbol.character(mol50000))
#   user  system elapsed 
#  1.518   0.000   1.518 
system.time(split_chem_elements(mol50000))
#   user  system elapsed 
#  0.435   0.000   0.436 
system.time(match.symbol2(mol50000))
#   user  system elapsed 
#  0.117   0.000   0.117 

-- 
Best regards,
Ivan



More information about the R-help mailing list