How to Find Directory & File Name from Path in R (2 Examples)
In this R programming tutorial you’ll learn how to extract file and directory name from a path.
Example 1: Returning Name of Directory from Path
my_path <- "C:/Users/Data Hacks/Desktop/example folder/example file.csv" # Exemplifying path to folder & file |
my_path <- "C:/Users/Data Hacks/Desktop/example folder/example file.csv" # Exemplifying path to folder & file
dirname(file_path) # Using dirname() function # [1] "C:/Users/Data Hacks/Desktop/my directory" |
dirname(file_path) # Using dirname() function # [1] "C:/Users/Data Hacks/Desktop/my directory"
Example 2: Returning Name of File from Path
basename(file_path) # Using basename() function # [1] "my file.txt" |
basename(file_path) # Using basename() function # [1] "my file.txt"