6 Matching Annotations
  1. Aug 2020
    1. Exercise 19.4.3

      jrnlod, your answer to this question is particularly great, thank you!!

      I added

      if (x == 0) { print(x) }

      as I don't think 0 should be a fizz/buzz Is there a less manual way to do this?

  2. Jul 2020
    1. Exercise 14.5.2 question 2 We have not learned 'unlist' yet, we learn that only in Ch 21

      I got a list and ordered it using the following code, but it takes only the first word in each sentence (I guess), and str_extract_all doesn't work at all. Any other ideas?

      tibble(word = (str_extract(sentences, boundary("word")))) %>% mutate(word = str_to_lower(word)) %>% count(word, sort = TRUE) %>% head(5)

      A tibble: 5 x 2

      word n

      <chr> <int>

      1 the 262

      2 a 72

      3 he 24

      4 we 13

      5 it 12

    2. Exercise 12.4.3.2. We learn str_split only in the next section. My rather inelegant solution was: has_apo <- sentences %>% str_subset("\'") has_ap_sep <- has_apo %>% tibble(sentence = has_apo) %>% extract(sentence, c("before", "apo", "after"), "([A-Za-z]*)(\')([A-Za-z]+)", remove = FALSE)

      cf https://en.wiktionary.org/wiki/Category:English_contractions There are some contractions where the apostrophe appears as the first character, so use * not + in the first [A-Za-z] expression

    1. Instead of using year, month, day, hour, you can join on only 'origin' and 'time_hour'

    2. The result shows that this is in fact not a primary key, as n > 1 is very high. Why?

      One of the variables is named 'n', so first rename that variable, then check:

      babynames %>% rename(no = n) %>% count(year, sex, name, prop) %>% filter(n > 1) %>% nrow()

      [1] 0

  3. May 2020
    1. Exercise 5.3.3

      Your solution works, but we have not been taught the mutate command yet. Given the commands we have already been taught, you can get the same results from: arrange(flights, desc(distance / air_time))