Extract numeric part of strings of mixed numbers and characters in R

后端 未结 4 606
傲寒
傲寒 2020-11-28 07:22

I have a lot of strings, and each of which tends to have the following format: Ab_Cd-001234.txt I want to replace it with 001234. How can I achieve

4条回答
  •  一个人的身影
    2020-11-28 08:14

    gsub Remove prefix and suffix:

    gsub(".*-|\\.txt$", "", x)
    

    tools package Use file_path_sans_ext from tools to remove extension and then use sub to remove prefix:

    library(tools)
    sub(".*-", "", file_path_sans_ext(x))
    

    strapplyc Extract the digits after - and before dot. See gsubfn home page for more info:

    library(gsubfn)
    strapplyc(x, "-(\\d+)\\.", simplify = TRUE)
    

    Note that if it were desired to return a numeric we could use strapply rather than strapplyc like this:

    strapply(x, "-(\\d+)\\.", as.numeric, simplify = TRUE)
    

提交回复
热议问题