Obtain List of Directories in R (2 Examples) | list.dirs Function Explained
In this R tutorial you’ll learn how to obtain a list of directories and folders contained at a specific path.
Table of contents:
You’re here for the answer, so let’s get straight to the examples!
Creation of Exemplifying Data
Consider the following exemplifying path and working directory:
my_path <- "C:/Users/Joach/Desktop/my directory" # Example directory
As you can see in the previously shown screenshot, our example directory contains three folders.
Let’s create a list of the folder names in our working directory using the R programming language…
Example 1: Get List of Full Directory Paths Using list.dirs() Function
The following R programming code shows how to get a list of all subdirectories stored in a working directory on a computer.
For this, we can apply the list.dirs function to the path of our working directory. Note that we are also removing the first element of the output by using “[- 1]”, since the first element would contain the main directory name.
list.dirs(my_path)[- 1] # Get full directory paths # [1] "C:/Users/Joach/Desktop/my directory/folder 1" # [2] "C:/Users/Joach/Desktop/my directory/folder 2" # [3] "C:/Users/Joach/Desktop/my directory/folder 3"
As you can see based on the previous output of the RStudio console, we have created a character vector containing the names of our folders and the full file path.
Example 2: Get List of Folder Names in Directory Using setwd() & list.dirs() Functions
In Example 2, I’ll show how to return only the names of the folders stored in our working directory (i.e. without the file path).
For this, we first have to set the current working directory to the path where we want to extract the folder names:
setwd(my_path) # Apply setwd function
Next, we can use the list.dirs function to extract the names of our subdirectories. Note that we are also using the gsub function, because otherwise the folder names would have the prefix “./”.
gsub("\\./", "", list.dirs()[- 1]) # Get only folder names # [1] "folder 1" "folder 2" "folder 3"
After executing the previous R syntax, the names of all folders in our directory have been printed to the RStudio console.
Video, Further Resources & Summary
Do you want to know more about working directories? Then you may watch the following video which I have published on my YouTube channel. I’m explaining the R programming code of this tutorial in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to have a look at some of the related tutorials of this homepage. I have published several related articles already.
- Extract File & Directory Name from Path
- How to Delete a File Using R
- Rename Files Using R
- R Programming Examples
You have learned in this tutorial how to create a list of folders and subdirectories in the R programming language. If you have any further comments and/or questions, please let me know in the comments. Furthermore, don’t forget to subscribe to my email newsletter to receive updates on the newest articles.
Statistics Globe Newsletter