To check the existence of a directory, you can use the following code:
This code will return TRUE if a directory exists and FALSE otherwise.
dir.exists(file.path(mainDir, subDir))
This code will create a directory and if it exists already, it will return FALSE.
dir.create(file.path(mainDir, subDir))
setwd(file.path(mainDir, subDir))