Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting csv file names without extension R

Tags:

r

I have a folder with different files. What I'm trying to do is to extract file names without extensions of csv files only.

for example: if i have a folder with files

cp1.csv 
cp2.csv 
sd.exe 

I'd like to get a vector:

"cp1" "cp2" 
like image 788
Alex Kors Avatar asked Dec 15 '22 22:12

Alex Kors


2 Answers

You could use a list.files() gsub() combo

basenames<-gsub("\\.csv$","", list.files(pattern="\\.csv$"))
like image 189
MrFlick Avatar answered Jan 10 '23 00:01

MrFlick


An alternative to gsub would be file_path_sans_extension from "tools". Try:

library(tools)
file_path_sans_ext(list.files(pattern = "*.csv"))

Not much added here, but it's still a fun function to know about :-)

like image 38
A5C1D2H2I1M1N2O1R2T1 Avatar answered Jan 09 '23 23:01

A5C1D2H2I1M1N2O1R2T1