string - strsplit by integers in R -
suppose have strings such like:
"0cgy-gcsf6hr-f-m5" "100cgy-lps12hr-m-m2" "300cgy-notrt24hr-f-m2" "0cgy-6h-m1"
it's sort of complicated, want retrieve them to:
"gcsf", "lsp", "notrt", "notrt"
which means if there none of "gcsf", "lsp", "notrt", return "notrt". , integers before h/hr
6, 12, 24, 6
i have tried while frustrated. have ideas?
do mean this?
library(stringi) library(magrittr) s <- c("0cgy-gcsf6hr-f-m5", "100cgy-lps12hr-m-m2", "300cgy-notrt24hr-f-m2", "0cgy-6h-m1") stri_extract_all_regex(s, "[a-z]+(?=[0-9]+hr?-)") %>% unlist() %>% ifelse(is.na(.), "notrt", .) stri_extract_all_regex(s, "[0-9]+(?=hr?-)") %>% unlist()
Comments
Post a Comment