Get filename without extension in R

后端 未结 7 1856
野趣味
野趣味 2020-12-01 05:56

I have a file

ABCD.csv 

The length before the .csv is not fixed and vary to any length.

How can I extract the portion

7条回答
  •  -上瘾入骨i
    2020-12-01 06:37

    You can use sub or substr

    sub('\\.csv$', '', str1) 
    #[1] "ABCD"
    

    or

    substr(str1, 1, nchar(str1)-4)
    #[1] "ABCD"
    

    Using the 'file_path' from @JasonV's post

    sub('\\..*$', '', basename(filepath))
    #[1] "ABCD"
    

    Or

    library(stringr)
    str_extract(filepath,  perl('(?<=[/])([^/]+)(?=\\.[^.]+)'))
    #[1] "ABCD"
    

    data

    str1 <- 'ABCD.csv'
    

提交回复
热议问题