Lesson 3 of43
In Progress

Importing Data

Your Turn

You’ll be working with data on Oregon school enrollment by race/ethnicity.

Create a new project. Make sure you put it somewhere you’ll be able to find it again later!

Download the two files using the download.file() function into a data-raw folder (which you’ll need to create).

Create a new R script file where you’ll do all of your data cleaning work

Import the two spreadsheets into two data frames (enrollment_17_18 and enrollment_18_19)

Heads Up!

If you have issues opening the spreadsheet, note that you may need to add the argument mode = "wb" to the download.file() function, as follows (read more about why here). You can see the full code in the solutions section below if necessary.

Solutions
Learn More

You can read about all of the arguments for the download.file() function here.

To learn more about importing Excel files, check out the readxl package documentation. You’ll see, for example, ways to download only certain ranges of cells, which can be helpful when you have messy Excel data!

I’ve also written an article about cleaning messy data in R. There are many packages to deal with messy data (which often comes in the form of Excel spreadsheets), and I go through several in the post.

Have any questions? Put them below. David or Charlie Hadley will help you out!

  1. fyi – I was able to download both .xlsx files, but when creating objects with the 17-18 data I got this error message:
    > download.file(url = “https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-17-18.xlsx”,
    + destfile = “data-raw/enrollment-17-18.xlsx”)
    trying URL ‘https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-17-18.xlsx’
    Content type ‘application/octet-stream’ length 47088 bytes (45 KB)
    downloaded 45 KB

    > enrollment_17_18 %
    + clean_names()
    Error: Evaluation error: zip file ‘data-raw/enrollment-17-18.xlsx’ cannot be opened.

    I was able to download the file to my computer, then copy it into the ‘data-raw’ folder manually and turn it into an object with R script successfully. So I’m moving on with the assignment but thought you would want to know.

  2. I am having a problem just downloading the files. My code (with or without the mode = “wb”,):

    download.file(url = “https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx”,
    destfile = “data-raw-gd/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx”)

    and I get these two errors:

    Warning messages:
    1: In download.file(url = “https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx”, :
    URL https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx: cannot open destfile ‘data-raw/enrollment-18-19.xlsx’, reason ‘No such file or directory’
    2: In download.file(url = “https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx”, :
    download had nonzero exit status

    This also happens with my own project file links (from Tidy Tuesday).

    1. Do you have a folder called data-raw-gd? That’s where the download.file() function is trying to put the file it downloads, but if that folder doesn’t exist, it won’t work.

        1. Actually, take a look back at your destfile argument:

          destfile = “data-raw-gd/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx”

          It’s going to try to put the data in the data-raw-gd/going-deeper/raw/master/data-raw/ folder. Try this instead and let me know if it works:

          download.file(url = "https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx",
          destfile = "enrollment-18-19.xlsx")

          That will put the file in the root of the project you’re working in. You did create a completely new project for this course, correct?

          1. this is what worked:
            download.file(url=”https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx”,
            destfile= “data-raw-gd/enrollment-18-19.xlsx”)

          2. And I went through the steps to create a new project in a new Directory, but I am still saving everything in an “R in 3 mo” folder. I think I have some troubleshooting yet to do about keeping projects truly separate.

          3. Yeah, I would recommend one RStudio project for each distinct project. It’s a bit artificial now because you’re working on “projects” that are actually just materials for each course. We can discuss more this week in our live session!

  3. The excel file I am downloading is not getting opened in excel. The following dialog box appears-“file format or file extension not valid. Verify the file has not been corrupted and that file extension matches the format”.

  4. You recommended data cleaning in R script but reporting in RMarkdown. What if I want my report to be 100% reproducible by someone else? Should I then do all my data cleaning within RMarkdown?

    1. Either an R script file or an RMarkdown file will be reproducible because other people can run your code so it shouldn’t matter on that front. Others do their data cleaning in RMarkdown and that works fine as well. I just have a preference to do it in an R script file. In the end, it’s up to you!

  5. At about 9:38 you said “let me highlight all of these” because keyboard shortcuts are faster. Did you use CTRL A to select all or something else to select just that chunk/those lines?

  6. Hi I was trying to import data from two excels(field_data_AA and predictor season13July) where 4 spread sheets (name are Autumn, Spring, Winter and Summer. I found following messages. I first run code with Spring and data imported then run code with all seasons? It did not work. Can you suggest me please how can I fix cell reference in excel files ?

    > field_data_2018
    > field_data_2019 <- read_excel(path = "Data-raw/predictor season13July.xlsx",
    + sheet = "Autumn","Spring","Winter","Summer")
    Error: Can't guess format of this cell reference:
    Spring
    In addition: Warning message:
    Cell reference follows neither the A1 nor R1C1 format. Example:
    Spring
    NAs generated.

    Cheers
    Niger

    1. Hello Niger,

      The read_excel() function will only allow you to read in one sheet at a time, eg


      read_excel(path = "Data-raw/predictor season13July.xlsx", sheet = "Autumn")

      You would need to import each of these sheets separately and to then combine them together with bind_rows(). As you’re a member of R in 3 Months I’d recommend asking about how to combine these in a submission and I can help you.

      Thanks,

      Charlie

  7. Hello whenever i try to run my code it keeps giving me an error mesage;

    source(“~/.active-rstudio-document”, echo=TRUE)
    Error in source(“~/.active-rstudio-document”, echo = TRUE) :
    ~/.active-rstudio-document:6:107: unexpected symbol
    5:
    6: download.file(url=”https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-mode = “wb
    ^

      1. library(tidyverse)
        library(janitor)
        library(readxl)

        download.file(url=”https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-mode = “wb”,
        destfile = “data-raw/enrollment-18-19.xlsx”)

        download.file(url= “https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-mode = “wb”,
        destfile = “data-raw/enrollment-17-18.xlsx”)

        #import data

        enrollment_18_19 <- read_excel(path = "data-raw/enrollment-18-19.xlsx",
        sheet = "Sheet 1")

        enrollment_17_18 <- read_excel(path= "data-raw/enrollment-17-18.xlsx",
        sheet = "Sheet 1")

        enrollment_18_19 <- read_excel(path = "data-raw/enrollment-18-19.xlsx",
        sheet = "Sheet 1")

        enrollment_17_18 <- read_excel(path = "data-raw/enrollment-17-18.xlsx",
        sheet = "Sheet 1")

        1. Hello Esther,

          Ah! Okay, this is a slightly difficult thing to explain.

          There are two types of double quotation characters, the curly ones you’re using “ and the straight one ” which are what you need to use in R code. Almost without exception the curly bracket cannot be used in programming languages.

          The next issue is a missing closing quotation mark at the end of the URL and the argument “mode”. The code you need to write is as follows:


          library(tidyverse)
          library(janitor)
          library(readxl)

          download.file(url = "https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-18-19.xlsx",
          mode = "wb",
          destfile = "data-raw/enrollment-18-19.xlsx")

          download.file(url = "https://github.com/rfortherestofus/going-deeper/raw/master/data-raw/enrollment-17-18.xlsx",
          mode = "wb",
          destfile = "data-raw/enrollment-17-18.xlsx")

          #import data

          enrollment_18_19 <- read_excel(path = "data-raw/enrollment-18-19.xlsx", sheet = "Sheet 1") enrollment_17_18 <- read_excel(path= "data-raw/enrollment-17-18.xlsx", sheet = "Sheet 1") enrollment_18_19 <- read_excel(path = "data-raw/enrollment-18-19.xlsx", sheet = "Sheet 1") enrollment_17_18 <- read_excel(path = "data-raw/enrollment-17-18.xlsx", sheet = "Sheet 1")

          Thanks,

          Charlie