Reputation: 691
I have the following path
filePath <- "/data/folder1/subfolder1/foo.dat"
I'd like to get subfolder1
which is where foo.dat
locates. I saw solutions in other languages but haven't found one in R. What is the simplest way to do it? Thank you!
What I tried
> basename(filePath)
[1] "foo.dat"
> dirname(filePath)
[1] "/data/folder1/subfolder1"
Upvotes: 16
Views: 12227
Reputation: 318
This may not be the prettiest answer, but it will work for you:
unlist(strsplit(filePath, '/'))[length(unlist(strsplit(filePath, '/')))-1]
Upvotes: 3
Reputation: 1873
This may solve:
filePath <- "/data/folder1/subfolder1/foo.dat"
basename(dirname(filePath))
http://www.r-fiddle.org/#/fiddle?id=IPftVEDk&version=1
Upvotes: 31