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.
- 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.
library(tidyverse)
table4a
table4b
table_new2 <- table4a %>% inner_join(table4b, by = c("country"))
table_new2
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
- 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.
- 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.
table_new2a %>% gather(rate.1999, rate.2000, key = "year", value = "rate")
- Make a clustered bar graph displaying the data.
Note the use of the as.factor() function.
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.
library(stringr)
For the string “Today is the second quiz.”
Use an str_? R function to count the length of the string.
Use an str_ R function to change all of the letters to lower case.
Use an str_? R function to subset the string into separate words.
x <- "Today is the second quiz."
x
[1] "Today is the second quiz."
# a.
str_count(x)
[1] 25
# b.
str_to_lower(x)
[1] "today is the second quiz."
# c.
str_split(x, " ")
[[1]]
[1] "Today" "is" "the" "second" "quiz."
LS0tCnRpdGxlOiAiUXVpeiAyIHByZXBhcmF0aW9uIHNvbHV0aW9uIgpvdXRwdXQ6CiAgd29yZF9kb2N1bWVudDogZGVmYXVsdAogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQKICBwZGZfZG9jdW1lbnQ6IGRlZmF1bHQKLS0tCgpBbnN3ZXIgdGhlIGZvbGxvd2luZyBxdWVzdGlvbnMuCgpUaGVzZSBxdWVzdGlvbnMgcmVsYXRlIHRvIHRoZSBob21ld29yayBwcm9ibGVtIDEyLjIuMSBFeG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