
```{R}
library(tidyverse)
library(HumanitiesDataAnalysis)
```


```{R}

```

2.  The above code prints to the screen, but doesn’t change the ‘crews’.
    Make it so that that assignment happens to a **new** dataframe.

### Cleaning Code

**The following code chunks are all broken**. You may have to run
individual lines. Rather than copying and pasting, remember that the
easiest way to run short sets of code in R is to highlight and then hit
“control-enter” to force it to run.

To give three at the end:

```{R}
x == 2
x + 1
```

Fix the following line of code so it gives (2,3,4)

```{R}

c((1, 2, 3) + 1

```

Fix this line so that ‘count’ equals three.

```{R}

count = 1 + "2"

```

<div class="cell"
hash="Cleaning_and_Shaping_cache/json/unnamed-chunk-24_a0e8bb8003b3019e3970c3e114ec23c9">

```{R}
count = 1 + 2  
```


```{R}
count = 1 + 2  
```


```{R}
count = 1 + 2  
```

</div>

1.  Is the data you read in under shiptypes `tidy`?

<!-- -->

2.  (Hard, skippable) In the crews dataset bundled with the package,
    I’ve added ‘feet’ and ‘inches.’ Here’s the code that I used to do
    it. Two hard questions:

    -   Can you describe what is going on in each line of this?
    -   Can you find any examples of heights that this *fails* to
        detect? (One way would be using an ‘is.na()’ filter)

<div class="cell"
hash="Cleaning_and_Shaping_cache/json/unnamed-chunk-26_b5fe4ea129b6b3deabda1a0c1078c08a">

```{R}
crews = crews %>% mutate(
  feet = Height %>%
    str_replace(" ?ft.? ", "'") %>%
    str_replace("[',] ?.*", "") %>%
    as.numeric(),
  inches = Height %>%
    str_replace(" ?ft.? ", "'") %>%
    str_replace("[0-9]'? ?(1?[0-9]).*", "\\1") %>%
    as.numeric()
)
```


```{R}
crews = crews %>% mutate(
  feet = Height %>%
    str_replace(" ?ft.? ", "'") %>%
    str_replace("[',] ?.*", "") %>%
    as.numeric(),
  inches = Height %>%
    str_replace(" ?ft.? ", "'") %>%
    str_replace("[0-9]'? ?(1?[0-9]).*", "\\1") %>%
    as.numeric()
)
```

</div>

<div class="cell"
hash="Cleaning_and_Shaping_cache/json/unnamed-chunk-28_568de33b359eb3e4a85a839b3ad8e663">

```{R}
# For two:
crews %>%
  filter(is.na(feet), !is.na(Height)) %>% select(Height)
```


```{R}
# For two:
crews %>%
  filter(is.na(feet), !is.na(Height)) %>% select(Height)
```


```{R}
# For two:
crews %>%
  filter(is.na(feet), !is.na(Height)) %>% select(Height)
```

</div>

2.  Using the `pivot_wider` functions from the `tidyr` package, create a
    new data.frame that has rows corresponding to ship types and columns
    corresponding to years. (That is, the exact inverse of the
    data.frame you saw before.)

<div class="cell"
hash="Cleaning_and_Shaping_cache/json/unnamed-chunk-30_57216f3ae0dab8265241033215a14804">

```{R}
crews %>%
  mutate(year = lubridate::year(date)) %>%
  count(Rig, year) %>%
  KEEP_GOING()
```


```{R}
crews %>%
  mutate(year = lubridate::year(date)) %>%
  count(Rig, year) %>%
  KEEP_GOING()
```

</div>

<div class="cell"
hash="Cleaning_and_Shaping_cache/json/unnamed-chunk-32_3390b144dc4968ee2c16a7e5166ab3d2">

</div>

3.  There’s another dataset. It was created by the [Center for Spatial
    and Textual Analysis at Stanford](https://cesta.stanford.edu/). Read
    it in. We’ll be working with this data set more next week.

<div class="cell"
hash="Cleaning_and_Shaping_cache/json/unnamed-chunk-34_307791cdc158459d6de6c9ccdeed68f4">

```{R}
CESTA %>% head()
```


```{R}
CESTA %>% head()
```

</div>

This data is clearly not tidy. (Why not?) Use `pivot_longer` to turn it
into tidy data, and save it as a data.frame called “tidied”. (Hint: the
final frame will have a column called “year.” You may have to use
str_replace to remove Xs from the year name.)

Use the `?` command or the help pane to read about the function
`write_csv`. Save the result to disk: we’ll be exploring it more next
week.
