I have a file:
ABCD.csv
The length before the .csv
is not fixed and vary in any length.
How can I extract the portion before the .csv
?
If you have fixed-width data, use read. fwf() or readr::read_fwf() to import your data. Whether or not a file has a particular extension is irrelevant to R.
To get the extension of a file in R, use the file_ext() method. The file_ext() is not a built-in R method. To use the file_ext() method, you need to import the tools library. Now, you can use the file_ext() method.
Bookmark this question. Show activity on this post. In linux we can use file command to get the file type based on the content of the file (not extension).
There's a built in file_path_sans_ext
from the standard install tools package that grabs the file without the extension.
tools::file_path_sans_ext("ABCD.csv") ## [1] "ABCD"
basename
will also remove the path leading to the file. And with this regex, any extension will be removed.
filepath <- "d:/Some Dir/ABCD.csv" sub(pattern = "(.*)\\..*$", replacement = "\\1", basename(filepath)) # [1] "ABCD"
Or, using file_path_sans_ext
as Tyler Rinker suggested:
file_path_sans_ext(basename(filepath)) # [1] "ABCD"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With