What is a File Path?

File Path Concept

In order to load your data into R (really, RStudio, but remember they’re used a bit interchangeably), you need to know where the file exists on your computer. It’s location is known as a file path. Millennials have written many articles about how we’re baffled that those younger than us don’t understand file paths, but it makes sense since most things you interact with are searchable by keyword. As a consequence of our bafflement, we’re often not great at explaining file structures, but I shall endeavor to explain.

Files, like an Excel file, are stored on your computer in folders. When you first download something, it will likely go to your Downloads folder. Your Downloads folder may exist inside your User folder. The path to your file would then be User -> Downloads -> File.

That’s it. It’s just folders within folders. But if you aren’t used to organizing your files into folders and just leave everything in Downloads and then search for a file with the search bar, this may be a little foreign to you. Also, you should do this. Don’t just leave everything in Downloads.

File paths are just nested file folders that lead to where your file is, and when coding you specify the file path by separating the folder names with a slash. Here’s an example:

The file you are trying to get to is inside the yellow folder, which is inside the green folder, which is inside the purple folder, which is inside the blue folder. Your file path would be:

Blue/Purple/Green/Yellow/File

On your computer this might look something like:

User/griffinj/Documents/Workshop/File

So, the file path is just naming the nested folders starting with the outmost ones. You can think of it as literally having to tell someone where a physical folder is in a filing cabinet.

File Path in Practice

The only difference between the above paths and what you need to type into R is a little bit of syntax. R needs the path to be inside ” ” (quotes) and needs you to start with a / (slash), so that it knows to go looking for a file. So the alteration to the above would be: On your computer this might look something like:

“/User/griffinj/Documents/Workshop/File”

You also need to add the file extension, which is the bit at the end that says what type of file the file is. Some common extensions are:

  • .xlsx - an Excel file

  • .csv - a comma separated value file

  • txt - a text file

  • .docx - a Word file

For R to know what to look for you need to specify the file name and its extension. So now we have the final version of the file path as:

“/User/griffinj/Documents/Workshop/File.xlsx”

File Names Need to be Exact

You have to type the file name into R exactly the way it appears on your computer. So if you have a file named “this is a bad way to name a file - version-2.5.csv”, you would need to type exactly that.

It is often easier to copy and paste a file path than to type it out manually, which brings us to the next section.