Lab 7: Tidying Data
Pivot monkeymem and the flower files. Unite a few flight columns.
Work in a Quarto document. Do 7.1
and 7.2 first. Put the course CSV files in a
data/ folder of your project (see the Data section of the
course page).
library(tidyverse)
library(nycflights13)
1. Longer
- Load
monkeymem.csv. PivotWeek2throughWeek16longer. Each row should be one monkey-week. The week names go in one column; accuracy goes in another. - On
table4a, show apivot_longer()call that fails because the year names are written as numbers, then the call that works. In a sentence, why the first one fails. - Pivot
monkeymem.csvagain withnames_prefix = "Week"andnames_transformso the week column is an integer. In a sentence, what type was that column before the transform?
2. Wider, separate, unite
- Load
flowers1.csvwithread_delim("data/flowers1.csv", delim = ";"). Widen soVariablebecomes column names andValuefills the cells. Fix the decimal commas ifValueis not numeric. - Load
flowers2.csv. SeparateFlowers/Intensityinto two numeric columns. Unite those two columns again with a comma. - In
flights, selectmonth,day,hour, andminute. Unitehourandminuteintosd_time, then parse that column as a time. What happens, and why? Fix it withpaste0()orstringrso minutes stay two digits.