R - Check if a Directory Exists and Create if It does not Last Updated : 20 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Directories and sub-directories are accessed by their corresponding paths in the R Programming Language. It is easy to work with these in R and perform operations related to the creation, copy, and movement of folders and sub-folders within the system. In this article, we will see how to check if a directory exists and how to create a new directory if it does not exist using R Programming Language. Directory in use: Check if the existDirectory existsThe path corresponding to the main directory can be first stored in the working space. We can check if this directory exists, using the file.exists() method. This method returns a logical vector depicting whether the files specified by its argument exist in the space or not. If the file exists, it returns TRUE, otherwise FALSE is returned. Syntax: dir.exists(paths) Parameter: path - a character vector containing a single path name. Example: R sub_dir<-"test1" file.exists(sub_dir) Output: TRUE Creating a directory if it does not existIf the file exists the working directory is set to the path formed by the concatenation of the main and subdirectories respectively. Otherwise, the directory is created using the dir.create() method. This method returns a logical vector describing if the creation of the file succeeded for each of the files for which it was attempted. dir.create indicates failure if the directory already exists. Syntax: dir.create(path, showWarnings = TRUE, recursive = FALSE, mode = "0777") Parameter : path - a character vector containing a single path name.showWarnings - logical; should the warnings on failure be shown?mode - the mode to be used on Unix-alikes.Example: R # setting up the main directory main_dir <- "C:\\Users\\Vanshi\\Desktop\\gfg\\test" # setting up the sub directory sub_dir <- "abc" # check if sub directory exists if (file.exists(sub_dir)){ # specifying the working directory setwd(file.path(main_dir, sub_dir)) } else { # create a new sub directory inside # the main path dir.create(file.path(main_dir, sub_dir)) # specifying the working directory setwd(file.path(main_dir, sub_dir)) } Output: Comment More infoAdvertise with us Next Article R - Check if a Directory Exists and Create if It does not Y yippeee25 Follow Improve Article Tags : R Language R-FileHandling Similar Reads Copy and Create Destination Directory if it Does Not Exist in Linux A directory is a file system location for storing and organizing files. A Linux system has many directories, and users can create their own directories. Copying and creating a directory are useful operations for Linux users. To create a new directory, use the command: mkdir new_directory. The 'm' st 6 min read How to Create Directory If it Does Not Exist using Python? In this article, We will learn how to create a Directory if it Does Not Exist using Python. Method 1: Using os.path.exists() and os.makedirs() methods Under this method, we will use exists() method takes path of demo_folder as an argument and returns true if the directory exists and returns false if 2 min read mkdir: Cannot Create Directory : File Exists The error "Mkdir: Cannot Create Directory 'Directory': File Exists" occurs when attempting to create a directory using the mkdir command in a Unix-like operating system, but a file with the same name already exists in that location. To resolve this issue, you can either choose a different name for t 3 min read How to Create Directory in Linux | mkdir Command In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help y 7 min read How to Create a Folder if It Doesn't Exist in PHP ? We can easily create a folder in PHP, but before that, you have to check if the folder or directory already exists or not. So In this article, you will learn both to Check and Create a folder or directory in PHP. Methods: file_exists(): It is an inbuilt function that is used to check whether a file 3 min read Like