Chapter 2

Chapter 2

CSVs

Quiz 1
  1. If you import a CSV with read.csv(), what argument do you pass to make sure strings are not interpreted as factors?

    • Correct!
    • Incorrect.
    Submit Answer
  1. When exporting a csv, how do you get rid of NAs with the readr package?

    • Correct!
    • Incorrect.
    Submit Answer

Excel

Quiz 2
  1. If you import an Excel file and it has 5 buffer rows at the top before the real data starts, how do you deal with that using the readxl package?

    • Correct!
    • Incorrect.
    Submit Answer
  1. When exporting a csv, how do you get rid of NAs with the readr package?

    • Correct!
    • Incorrect.
    Submit Answer

Column names

What if you import in a spreadsheet with a space in the column name like below?

ABCDEFGHIJ0123456789
id
<fctr>
First name
<chr>
age
<dbl>
grade
<fctr>
60006Bob45NA
60007Linda44NA
60008Louise12seventh
60009Tina13eighth
60010Gene11sixth

How do you refer to the the First name variable/column with $ and not the []?

1
2
3
# Add to the line below
burgers$
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
burgers$`First name`

Clean up column name

Let’s create a new data frame burgers2 from burgers because we’re going to change it up.

Run the command on the First name variable/column to strip out the spaces and characters and replace them with periods.

1
2
3
4
5
6
7
# Modify the line of code below
burgers2 <- burgers
colnames(burgers2) <- _______(colnames(burgers2))
burgers2
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
burgers2 <- burgers

colnames(burgers2) <- make.names(colnames(burgers2))

burgers2

Rename a column

Use the special dplyr function to rename the id variable in burgers to employee_id

1
2
3
4
5
# Modify the line of code below
burgers2 <-
burgers2
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
burgers2 <- rename(burgers, employee_id=id)

burgers2

Filter out NAs

ABCDEFGHIJ0123456789
employee_id
<fctr>
name
<chr>
age
<dbl>
grade
<fctr>
60006Bob45NA
60007Linda44NA
60008Louise12seventh
60009Tina13eighth
60010Gene11sixth

Create a new data frame called kids by filtering out the NAs from grade column in burgers.

1
2
3
4
5
# Modify the line of code below
kids <- _______(burgers2, !____(______))
kids
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
kids <- filter(burgers2, !is.na(grade))

kids

Chapter 2

CSVs
Excel
Start Over