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
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"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top