Practice: Functions and Packages

As your code gets bigger, it might be hard to spot an error (a "bug" is what programmers call it politely). These examples let you practice debugging (finding and fixing errors in the code) for the code to run smoothly and correctly. Install R packages if needed for the examples to run. This exercise does not count toward your grade. It is just for practice!

Exercises

  1. Why does this code not work?
  2. my_variable <- 10
    my_varıable #> Error in eval(expr, envir, enclos): object 'my_varıable' not found

    Look carefully! (This may seem like an exercise in pointlessness, but training your brain to notice even the tiniest difference will pay off when programming)

  3. Tweak each of the following R commands so that they run correctly:

    library(tidyverse)
    
    ggplot(dota = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy))
    
    fliter(mpg, cyl = 8)
    filter(diamond, carat > 3)


  4. Press Alt Shift +  K. What happens? How can you get to the same place using the menus?


Source: H. Wickham and G. Grolemund, https://r4ds.had.co.nz/workflow-basics.html
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 3.0 License.

Last modified: Thursday, December 15, 2022, 4:45 PM