Getting .csv Data into R
- In order to get a .csv file into R, you can use read.csv, and as the only argument, then 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.
- In the sample below, the file on the computer is called menu.csv, therefore we used the script: read.csv("menu.csv")
- 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")
- Note that Mac users should use single forward slashes while Windows users have to use double back slashes.
- Alternatively, the file is on a website, and you can actually read it in straight from there by typing: read.csv("https://joeystanley.com/data/menu.csv Links to an external site.")
- 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!
- 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")
- 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.