Import Excel file

I downloaded an Excel file from the R Study Guide , at the bottom of the page, and saved it into this project.

I need to import it into R:

my_grants <- read_excel("nea_grants.xlsx")

Let’s take a look at it, first showing the top, then showing the bottom, then showing the structure of the dataset:

Top:

head(my_grants)

Now the bottom:

tail (my_grants)

Now the columns:

str(my_grants)
## tibble [519 × 14] (S3: tbl_df/tbl/data.frame)
##  $ Organization Name     : chr [1:519] "Arizona Theatre Company" "Childsplay, Inc." "Arts Foundation for Tucson and Southern Arizona" "University of Arizona" ...
##  $ Popular Name          : chr [1:519] "aka ATC" "aka" NA NA ...
##  $ Discipline / Field    : chr [1:519] "Theater" "Theater" "Design" "Museums" ...
##  $ Category              : chr [1:519] "Creativity / Presentation" "Creativity / Presentation" "Leadership" "Access" ...
##  $ Application Number    : chr [1:519] "00-3200-5049" "00-3200-5138" "00-4200-3047" "00-4400-4103" ...
##  $ City                  : chr [1:519] "Tucson" "Tempe" "Tucson" "Tucson" ...
##  $ State                 : chr [1:519] "AZ" "AZ" "AZ" "AZ" ...
##  $ Zip                   : chr [1:519] "85701-1909" "85281-5592" "85701-1800" "85721-0158" ...
##  $ Project Description   : chr [1:519] "To support the Shakespeare Project. This project will encompass productions of three plays by William Shakespea"| __truncated__ "To support premiere productions and the early development of a commissioned work. The project includes producti"| __truncated__ "To support a national competition for design of the Gateway Overpass, a major transportation infrastructure pro"| __truncated__ "To support increased and diversified public programming for the Center for Creative Photography's traveling exh"| __truncated__ ...
##  $ Fiscal Year           : num [1:519] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
##  $ Congressional District: num [1:519] 3 9 3 3 3 9 3 7 7 7 ...
##  $ Grant Amount          : num [1:519] 40000 25000 50000 40000 45000 20000 30000 15000 1200 8250 ...
##  $ Grant From Date       : POSIXct[1:519], format: "2000-01-01" "2000-01-01" ...
##  $ Grant To Date         : POSIXct[1:519], format: "2002-11-30" "2000-12-31" ...

Rename some columns

I’ll rename some columns to make them easier to work with:

new_grants <- 
  my_grants %>%
  rename ( organization = "Organization Name", 
           aka = "Popular Name", 
           discipline = "Discipline / Field")

A shortcut for all of this is in a new library called janitor.

new_grants <- 
  my_grants %>%
  janitor::clean_names()

(If you get "error: ‘janitor’ not found, then you should install it in the console, not here! )

Common problems

Step 1, reading the data

  • nea_grants.xlsx was not in your project folder - it had been saved somewhere else
  • readxl wasn’t loaded as a library
  • The chunk containing the libraries hadn’t been run yet – at first, you might just run all chunks every time using the right arrow rather than the down arrow or the shortcut keys.
  • “nea_grants.xlsx” was either misspelled or not in quotation marks.

Step 2, printing the top and bottom

I think some folks forgot the “str” command. It’s not a big deal, but if you get an error, don’t keep going – try to figure out what it is.

Also, I discourage a lot of Googling around for the answers. I saw some folks doing things that are outside the “tidyverse” that they apparently copied from elsewhere. That’s great, but for now try to keep to the program.

Step 3, renaming

You all found something that I’d not known – that you can use regular quotation marks in the rename statement ! I didn’t know that. I don’t think it will work in every context, but I’m not sure. Quotes usually mean “take this literally – use these exact letters and numbers”, not “a variable called this”.

  • Forgot to enter the and then operator, %>%
  • Put old name = new name, not new name = old name.

Some people skipped entirely - I don’t know if that’s because you were uncomfortable or not.

The big issue

Please don’t scrimp on writing out your documentation! You should be able to get back to everything you do, and be able to describe in each step what you’re about to do. See this as an example, but sometimes it’s much more involved. Documenting what you are doing is more important at this stage than actually doing it!

LS0tCnRpdGxlOiAiSG9tZXdvcmsgMSIKYXV0aG9yOiAiU2FyYWggQ29oZW4iCmRhdGU6ICIzLzEvMjAyMSIKb3V0cHV0OiAKICAgaHRtbF9kb2N1bWVudDoKICAgICB0aGVtZTogam91cm5hbAogICAgIHRvYzogdHJ1ZQogICAgIHRvY19mbG9hdDogdHJ1ZQogICAgIGNvZGVfZG93bmxvYWQ6IHRydWUKICAgICBkZl9wcmludDogcGFnZWQKLS0tCgpgYGB7ciBzZXR1cCwgaW5jbHVkZT1GQUxTRX0Ka25pdHI6Om9wdHNfY2h1bmskc2V0KGVjaG8gPSBUUlVFKQoKbGlicmFyeSh0aWR5dmVyc2Up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