Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: extract directory out of a path [duplicate]

Tags:

r

Possible Duplicate:
How do I extract a file/folder_name only from a path?

May I ask you how I can get the last subdirectory of a path. For example I want to get the subdirectory "7" and the following code fails:

Path <- "123\\456\\7"
Split <- strsplit(Path, "\\") # Fails because of 'Trailing backslash'
LastElement <- c[[1]][length(Split[[1]])]

Thank you in advance

like image 471
Apostolos Polymeros Avatar asked Jul 24 '12 21:07

Apostolos Polymeros


1 Answers

You could also use the built-in function basename:

basename(Path)
[1] "7"
like image 70
James Avatar answered Oct 31 '22 01:10

James