Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

This may be a very easy question for someone - I am able to use list.files() to obtain a list of files in a given directory, but if I want to get a list of directories, how would I do this? Is it somehow right in front of me as an option within list.files()?

Also, I'm using Windows, so if the answer is to shell out to some Linux/unix command, that won't work for me.

.NET, for example, has a Directory.GetFiles() method, and a separate Directory.GetDirectories() method, so I figured R would have an analogous pair. Thanks in advance.

1 Answer

0 votes
by
edited

To obtain a list of directories within a directory, you can use the list.dirs function from the base package as follows:

list.dirs(path = "Path of Directory", full.names = TRUE, recursive = TRUE)

For example:

list.dirs(path = "C:\\Users\\DELL\\Desktop", full.names = TRUE, recursive = TRUE)

[1] "C:\\Users\\DELL\\Desktop"                                                                                                                          

 [2] "C:\\Users\\DELL\\Desktop/assignment"                                                                                                               

 [3] "C:\\Users\\DELL\\Desktop/java"                                                                                                                     

 [4] "C:\\Users\\DELL\\Desktop/java/New folder"                                                                                                          

 [5] "C:\\Users\\DELL\\Desktop/java/programs"                                                                                                            

 [6] "C:\\Users\\DELL\\Desktop/R"  

If you want to explore more in R programming then watch this R programming tutorial for beginner:

            

Related questions

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 16, 2019 in Python by Sammy (47.6k points)

Browse Categories

...