Possible duplicate:How to extract file / folder_name from path only?
May I ask you how I can get the last subdirectory of the path. For example, I want to get the subdirectory "7", and the following code does not work:
Path <- "123\\456\\7" Split <- strsplit(Path, "\\") # Fails because of 'Trailing backslash' LastElement <- c[[1]][length(Split[[1]])]
Thank you in advance
You can also use the built-in function basename:
basename
basename(Path) [1] "7"
You must add a second pair \\to output \to the regular expression:
\\
\
> Path <- "123\\456\\7" > Split <- strsplit(Path, "\\\\") > Split[[1]][length(Split[[1]])] [1] "7"