To find the name of a directory in R, you can use the basename()
function along with the dirname()
function. basename()
will return the final component of a path (i.e. the directory name) and dirname()
will return the path of the parent directory.
Here's an example code snippet:
115 chars4 lines
In this example, path
is the path to the directory whose name we want to extract. dir_name
is a variable that stores the extracted directory name. The basename()
function extracts the name of the parent directory (i.e. "my_analysis") and then the dirname()
function extracts the name of the grandparent directory (i.e. "data").
If you simply want to extract the name of the directory without going up the parent or grandparent directory, you can just use the basename()
function on the path. Here's an example:
101 chars4 lines
This code snippet will print "my_analysis", which is the name of the directory stored in the path
variable.
gistlibby LogSnag