--- title: "Quiz 2 preparation solution" output: word_document: default html_notebook: default pdf_document: default --- Answer the following questions. These questions relate to the homework problem 12.2.1 Exercise 2. Use what you know from Chapter 13 to answer the questions for *table4a* and *table4b*. 1. Using *table4a* and *table4b* from the *tidyverse* R package, merge the two dataframes into one and create a column, rate per 10000 variable, for each year. ```{r message=FALSE} library(tidyverse) table4a table4b ``` ```{r} table_new2 <- table4a %>% inner_join(table4b, by = c("country")) table_new2 ``` ```{r} table_new2a <- table_new2 %>% mutate( rate.1999 = (`1999.x`/`1999.y`)*10000, rate.2000 = (`2000.x`/`2000.y`)*10000 ) %>% select(country, rate.1999, rate.2000) table_new2a ``` 2. Is your final dataframe for questions 1 tidy? Yes or no, explain. **Answer:** No. The *table_new2a* is not tidy. The rates are in two columns. 3. Convert your final dataframe for question 1 into a tidy dataframe with three columns country, year and rate. **Answer:** Gather the columns into year and rate columns. ```{r} table_new2a %>% gather(rate.1999, rate.2000, key = "year", value = "rate") ``` 4. Make a clustered bar graph displaying the data. Note the use of the as.factor() function. ```{r} table_new2a %>% gather(rate.1999, rate.2000, key = "year", value = "rate") %>% ggplot(aes(x = as.factor(year), y = rate, fill = country) ) + geom_bar(position="dodge", stat="identity") ``` The next question relates to Chapter 14. ```{r} library(stringr) ``` 5. For the string “Today is the second quiz.” a. Use an *str_?* R function to count the length of the string. b. Use an *str_* R function to change all of the letters to lower case. c. Use an *str_?* R function to subset the string into separate words. ```{r} x <- "Today is the second quiz." x # a. str_count(x) # b. str_to_lower(x) # c. str_split(x, " ") ```