Chapter 8 Minihacks

Minihacks are practice problems for you to challenge yourself and learn the material more deeply. Give them a shot!

8.1 Minihack 1: R Markdown

  1. Create an R Markdown document called minihacks. Save it in your tools -> scripts folder.

  2. Try rendering your R Markdown document by clicking knit. If it doesn’t render correctly, try to figure out why it didn’t.

8.2 Minihack 2: Arithmetic Commands

  1. Use R to calculate \(\frac{(102 + 68) \times (3 + 2) + 1250}{50}\) and assign the result to a variable called x.

  2. Assign the numbers 10, 20, and 30 to a vector called y.

  3. Before running any code, determine what you think adding x to y would result in. Then, using R, add x to y.

8.3 Minihack 3: Functions

  1. Assign the string "I AM NOT YELLING" to a variable called exclamation.

  2. Use the function tolower() to convert every letter of exclamation to lower case. Assign the result to exclamation.

  3. Use the capitalize() function from the Hmisc package to capitalize the first letter of exclamation.

8.4 Minihack 4: Help Documentation

  1. I wanted to create a vector of 5 values between 10 and 50 using seq(), but the code I wrote is creating a vector of 9 values between 10 and 50. I believe it has something to do with the arguments I used, but I can’t remember how to access the help documention to check. Without changing the values (i.e., 10, 50, and 5), can you fix my code?
seq(from = 10, to = 50, by = 5)
## [1] 10 15 20 25 30 35 40 45 50

8.5 Minihack 5: Data Frames

  1. Download the Marvel character dataset to your computer. Put the data file in your tools -> data folder.

  2. Import the data into R and assign it to a variable called marvel_data.

  3. Ah! The value for the number of appearances of Spider-Man seems to be an error! It should be 4043 not 40430! Use square brackets ([]) to replace the erroneous value with the correct value (hint: The value is stored in the first row of the eighth column).

  4. Using mean() and dollar sign notation (data$column), calculate the average number of appearances for all of the Marvel characters. Assign the result to a variable called mean_appearances.

  5. Install and load the package ggplot2.

  6. If you succesfully completed the proceeding steps, you should be able to run the following code without producing an error. If you get an error, try to figure out why you are receiving the error.

ggplot(marvel_data, aes(x      = reorder(align, -appearances), 
                        y      = appearances,
                        fill   = align)) +
  geom_bar(stat = "summary", fun.y = "mean") +
  geom_point(shape    = 21,
             alpha    = .7,
             position = position_jitter(w = 0.4, h = 0)) +
  geom_hline(yintercept = mean_appearances,
             linetype   = "twodash",
             lwd        = 1,
             colour     = "firebrick") +
  annotate(geom   = "text", 
           x      = 3, 
           y      = 800, 
           size   = 5,
           label  = paste("Mean = ", round(mean_appearances, 2)),
           colour = "firebrick") +
  scale_fill_viridis_d() +
  theme_bw(base_size = 15) +
  theme(legend.position = "none") + 
  labs(title    = "Alignment and Appearances",
       subtitle = "Marvel character appearances by alignment",
       x        = "Alignment",
       y        = "Appearances")