Getting Data Into R .CSV Files

1. In order to get a .csv file into R, you can use read.csv, and as the only argument, put the path to the file you want to read in within quotation marks. Ideally, the file should be close to, if not in the same folder as, this script. That way you only need to type a relative path.

2. In the sample below, the file on the computer is called menu.csv, therefore we used the script: read.csv("menu.csv")

3. If the file is somewhere else on your computer, you can type a full path. For example, on a MAC you would type: read.csv("/Users/joeystanley/Desktop/menu.text")

4. Note that Mac users should use single forward slashes while Windows users have to use double back slashes.

5. Alternatively, the file is on a website, and you can actually read it in straight from there by typing: read.csv("http://joeystanley.com/downloads/menu.csv")

6. When you do this, you will start to see the contents of your file displayed in your R console (the bottom left portion of the RStudio screen). Hooray! You just read your data into R!

7. Unfortunately, all it did was read the file in and forget it. Computers do exactly as they are told. What we did not tell R was to remember the contents of the file. So, let's create a new variable called menu and save the contents of the menu.csv file into that variable by typing: menu<-read.csv("menu.csv")

8. Okay, so now we have a new menu object that has the full contents of that file. Before we move on to working with that file, let's see how to read in files that are in the other formats.