
READ CSV RSTUDIO CODE
Run the following code to read the data into R: library(readr) The argument to read_csv() is the location of the file, relative to your working directory. Read the CSV with the read_csv() function from the readr package. Use RStudio’s “Files” pane to navigate to where you downloaded the data. This CSV file contains country-by-year level data on GDP, population, and life expectancy.


The use of the comma as a field separator is the source of the name for this file format.ĭownload the data for this lesson using this link. Each record consists of one or more fields, separated by commas. You’ll see that RStudio calls the setwd() function with the appropriate path for your operating system.Ī comma-separated values (CSV) file stores tabular data (numbers and text) in plain text. Use the “Files” pane to navigate into the folder where you want to be working, click the “More” button and select “Set As Working Directory”. RStudio makes navigating your file system a little easier.
READ CSV RSTUDIO MAC
Unfortunately paths on Windows are a little different compared to paths on Mac and Linux. To change the working directory use the setwd() function. This is likely different from the output you see running the same function on your computer. Often it is helpful to know where R is working from, to see the current working directory use the getwd() function: getwd() # "/home/travis/build/hbs-rcs/R_Intro-gapminder/content/post"Īs R compiles this lesson, it’s working from the directory listed above. Read_csv("my-data.csv") # Error: 'my-data.csv' does not exist in current working directory ('/home/travis/build/hbs-rcs/R_Intro-gapminder/content/post'). If that file does not exist in that directory you’ll receive an error message.

So, if you tell R to open a file named my-data.csv it will look in the current working directory to see if it can find that file. This is the folder where R is working from. Whenever you are working in R, your R session has a current working directory. I found this introduction to working with files and folders quite helpful. One of the trickiest parts of working with R is telling it where to find files on disk.
