Home > database >  Subset strings of the folder name in a vector
Subset strings of the folder name in a vector

Time:08-19

I have the following vector of directories:

c(".", "./LRoot_1", "./LRoot_1/asd", "./LRoot_1/LClass_copepodo", 
"./LRoot_1/LClass_shadow", "./LRoot_2", "./LRoot_2/asd", "./LRoot_2/LClass_bolha", 
"./LRoot_2/LClass_cladocera")

I would to like get the the names of each element after LClass_ into a new vector.

Thanks

CodePudding user response:

result = str_extract(x, pattern = "(?<=LClass_).*")
result
# [1] NA          NA          NA          "copepodo"  "shadow"    NA         
# [7] NA          "bolha"     "cladocera"

CodePudding user response:

In base R

 sub("LClass_", "", grep("LClass_", basename(v1), value = TRUE))
[1] "copepodo"  "shadow"    "bolha"     "cladocera"

Or with

regmatches(v1, regexpr("(?<=LClass_).*", v1, perl = TRUE))
[1] "copepodo"  "shadow"    "bolha"     "cladocera"
  • Related