

Number_of_rows = sample(100:500, size = 25, replace = FALSE) # Check if the folder "Raw Data" exists in the working directory, if it doesn't exists create it # Set the working directory to any folder in your computer

Ifelse(!dir.exists("Data"), dir.create("Data"), "Folder exists already")Ĭopy the code below and write the R code to complete the exercises in the lines highlighted #Check if the folder "Data" exists in the current directory, if not creates it #Check if the file "raw data.csv" exists in the working directory To change the current working directory we need to use a function called setwd ( ). If we run this function we will get the current working directory or current path of the R script. For getwd ( ), no need to pass any parameters. , *, $ these are called metacharacters in regular expressions. If you want to learn more about regular expressions you can watch these two videos: Regular Expressions in general and Regular Expressions in R. If we want to check the current directory of the R script, we can use getwd ( ) function. If you’re wondering what are the symbols ^. List.files(pattern = "^Product(.*)xlsx$") #list files that start with the word "Product" followed by anything and ending with the word "xlsx": #list all files that contain the word "Product" If you get a very long list, you can filter the results using the pattern argument of the list.files() function
#R get file path full#
#get the full name (path and file name) of each file #list of subfolders and files within the subfolders List.files (path = "C:/Folder/Subfolder1/Subfolder2")

You might also want to create a list of all the files or folders in a directory Creating a list of files and folders in a directory Tip: If you are using RStudio you can use the shortcut Ctrl + Shift + H to browse to the desired directory. Setwd(paste0(getwd(), "/Statistical Analysis")) #Set the working directory to a subfolder within the current working directory For example, let’s say you want to set the working directory to a subfolder called “Statistical Analysis”: If you want to setup the working directory to a subfolder within your current working directory you don’t have to specify the whole path. Setwd("C:/Documents and Settings/Folder name")
#R get file path how to#
Therefore, the first thing you need to know is how to get and setup your working directory. Unless you specify it otherwise, all files will be read and saved into the working directory. In this post I’ll cover how to work with files and folders in R.
