FilipeTeixeira
FilipeTeixeira

Reputation: 1160

Extract segment of filename

I'm trying to extract a filename and save the dataframe with that same name. The problem I have is that if the filename for some reason is inside a folder with a similar word, stringr will return that word as well.

filename <- "~folder/testdata/2016/testdata 2016.csv"

If I run this:

library(stringr) str <- str_trim(stringr::str_extract(filename,"[t](.*)"), "left") it returns testdata/2016/testdata 2016.csv when all I want is testdata 2016. Optimally it would even be better to get testdata2016.

I've been trying several combinations but there has to be a simpler way of doing this. If there was a way of reading the path from right to left, starting at .csv stop at /, I wouldn't have this issue.

Upvotes: 1

Views: 641

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78792

Plenty of help in base R (tools pkg comes with the default R install):

gsub(" ", "",
  tools::file_path_sans_ext(
    basename("~folder/testdata/2016/testdata 2016.csv")))

Upvotes: 2

PKumar
PKumar

Reputation: 11128

You can have below approaches:

library(stringr)
str_replace(str_extract(filename,"\\w*\\s+\\w*(?=\\.)"),"\\s+","")

str_replace_all(basename(filename),"\\s+|\\.csv","")

You can use basename approach as suggested by Benjamin.

?basename:

basename removes all of the path up to and including the last path separator (if any).

Output:

[1] "testdata2016"

Upvotes: 2

Related Questions