Import data into R

Overview

Teaching: 20 min
Exercises: 20 min
Questions
  • How to get my data file into R?

Objectives
  • Import your .csv file to RStudio with its Url.

  • Import your .csv file to RStudio with command.

Two options to let RStudio read your data

After the growing season, you have your data downloaded from KnowPulse in a comma separated value .csv format file. In the next step, you want to import your .csv file into Rstudio for further analysis.

RStudio can read files in various formats, see Importing Data with RStudio for more details.

Option 1: Import your .csv file with its path

Step 1

Import your downloaded .csv file on bottom right panel to Rstudio, so you will be able to see the Url of your file. Screenshot of main code listing

Step 2

Then you can preview your data; meanwhile, more Import Options are available for you to rename your file or choose which sheet you would like RStudio to view if your file has multiple pages. Screenshot of main code listing

Step 3

It is intuitive to hit Import button, which is an option to import your file; as an alternate, you can also copy-paste the code into the editor panel. Screenshot of main code listing

Step 4

Inside the bracket beside the library, readr is the package that is used to read your file. So you want to run both lines of code.

Screenshot of main code listing

Right now, your file is successfully imported to RStudio; you can continue on your data analysis. Screenshot of main code listing

Option 2: Import your read.csv with code

The following command can also be used to import your file. Let us call your file mydata to make it simple.

Step 1

mydata <- read.csv(file.choose(), header=T)

Step 2

Screenshot of main code listing

Now you can see your entire dataset showing up. Screenshot of main code listing

Key Points

  • read_csv can only be used .csv files.