Wilson Souza
Wilson Souza

Reputation: 860

Subset strings of the folder name in a vector

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

Upvotes: 0

Views: 29

Answers (2)

akrun
akrun

Reputation: 886928

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"

Upvotes: 1

Gregor Thomas
Gregor Thomas

Reputation: 145745

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

Upvotes: 1

Related Questions