library(tidyverse)
library(googlesheets4)AE04 - Getting to Know Your Data Types
Connecting to Google Sheets
Getting to Know You: Data Types
Purpose
In this application exercise you’ll import data directly from a live Google Sheet, then do some data cleaning as necessary.
Learning objectives
- Import a dataset from Google Sheets with googlesheets4, including authenticating with your own Google account
- Identify the data type R assigns to a variable and compare it to what that variable conceptually should be
- Diagnose and repair errors
Data
The data come from the “Getting to know you” survey everyone filled out at the start of the semester. Before you import anything, open the survey itself — and look through the questions and answer choices. You’ll be comparing what you see there to what shows up in the data later in this AE.
Setup
A quick word on googlesheets4
googlesheets4 reads a Google Sheet straight into R as a data frame, avoiding the need to download and reimport a copy. This particular Sheet is not public — it’s only viewable if you’re signed into a Google account that has access to it.
The first time you run a chunk that calls read_sheet(), R will kick off an authentication flow: a browser window opens and asks you to sign into a Google account, then grants the Tidyverse API Packages app permission to read your Sheets. A few things to expect:
- Sign in with your @csuchico.edu Google account — that’s what has access to this Sheet.
- Once you’ve signed in, googlesheets4 caches that token, so you shouldn’t have to repeat this every single time you render.
- If you get a “permission denied” error instead of your data, it usually means you authenticated with the wrong account — run
googlesheets4::gs4_auth()on its own to be prompted again and pick a different one.
For more than this AE covers:
Step 1: Import
Google Forms turns each question into a column name, which makes for very long column names. We’ll rename them as we import to something easier to type.
raw <- read_sheet("https://docs.google.com/spreadsheets/d/1e8XSidxeT3SIE10g1jiEuALRa7REpmYU64_3V3TNKKs/edit?usp=sharing") %>%
rename(
timestamp = Timestamp,
zipcode = `What is the zipcode of your hometown?`,
stats_before = `Have you taken any statistics courses before?`,
prog_experience = `How much experience do you have with programming?`,
languages = `If you've programmed before, which languages have you used, and how comfortable do you feel with each? If you haven't programmed before, please leave this question blank.`,
data_interest = `What types of data interest you?`,
learn_best = `How do you learn best?`,
commute_min = `How long does it take you to commute from home to this classroom, in minutes? Please enter a number only.`
)Confirm the token is cached
googlesheets4 (via the gargle package) caches your token in a local folder on your computer (typically ~/.R/gargle/gargle-oauth). This means you shouldn’t have to re-login to google, just specify to use the stored token.
- Fully quit RStudio.
- Reopen RStudio in this
AEproject - Run the import chunk again.
- Read what happens. You should see something about reusing a cached token rather than a fresh browser sign-in. If gargle shows you a menu of cached accounts in the console, read it and choose your @csuchico.edu account, rather than signing in again.
Step 2: Data type audit
Before working with the data, assess these two variables: what type does R currently see each as, and what should it be, given the question that was actually asked?
“How long does it take you to commute from home to this classroom, in minutes? Please enter a number only.”
# add code hereIs that what you’d expect, given the question?
# add code hereWhat’s going on? unlist() this variable and look at the unique() values.
# add code here“How much experience do you have with programming?”
# add code hereIs this variable ordinal? Does everything you’d expect to see actually show up?
Skim the rest
# add code hereFor each of the other variables — is R’s type what you’d expect, given the question that was asked? We won’t work with them further in this AE, but you should still be able to answer this for each one.
Step 3: Apply fixes
Commute time
While we could try to coerce list → character → numeric, it’s easier here to specify data types on read.
survey <- read_sheet("https://docs.google.com/spreadsheets/d/1e8XSidxeT3SIE10g1jiEuALRa7REpmYU64_3V3TNKKs/edit?usp=sharing",
col_types = c("Tccccccn")) %>%
rename(
timestamp = Timestamp,
zipcode = `What is the zipcode of your hometown?`,
stats_before = `Have you taken any statistics courses before?`,
prog_experience = `How much experience do you have with programming?`,
languages = `If you've programmed before, which languages have you used, and how comfortable do you feel with each? If you haven't programmed before, please leave this question blank.`,
data_interest = `What types of data interest you?`,
learn_best = `How do you learn best?`,
commute_min = `How long does it take you to commute from home to this classroom, in minutes? Please enter a number only.`
)Warning message: NAs introduced by coercion
Always keep an eye out for this, and ask yourself whether it’s expected, given what you found in Step 2.
# add code hereRecode & reorder prog_experience
The forcats package has a lot of useful functions for wrangling factors. Here are some refs for the functions we’re going to use:
- First, confirm what’s actually in the data.
# add code hereThen compare against the published survey. Is everything you’d expect to see actually there?
Convert the variable to a factor with the current recorded levels.
# add code here- Now that it’s reading as a factor, use
fct_expand()to add any level that’s missing.
# add code here- Last, respect the ordinal nature of this variable by putting the levels in increasing order.
# add code here