R For Data Science | by: Wickham & Grolemund

Chapter 2 | Workflow Basics

2.1 Coding basics

Read the following line of code as:

–> # Object name “gets” value

object_name <- value

The arrow in this case is an assignment. You will make lots of assignments, and <- is a pain to type. You can save time with RStudio’s keyboard shortcut: Alt + - (the minus sign).

# alt and - at same time makes arrow like this: 

hi<-2 
  
  my<-3 
  
  alt_minus<-4 

2.2 Comments

Anytime you change the default of a code, i.e. you change the binwidth for a histogram – state why you did that as a #comment.

2.4 Calling functions

seq(1, 10, 2)
## [1] 1 3 5 7 9
seq(1, 10)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(0, 10, 2)
## [1]  0  2  4  6  8 10
x <- "hello"

2.5 Exercises

1 | Why does this code not work?

my_variable <- 10
my_variable
## [1] 10

This code did not work because my_variable was spelled wrong.

2 | Tweak each of the following R commands so that they run correctly:

libary(todyverse)

mpg |>
  ggplot()+
  geom_point(aes(displ, hwy))

I could not get geom_smooth() to work here. IDK why.

3 | Press Option + Shift + K / Alt + Shift + K. What happens? How can you get to the same place using the menus?

You can change your quickkeys in the tools pane.

Alt + shift + k will show you all of your quickkeys SUPER FAST!

4 | Let’s revisit an exercise from the Section 1.6. Run the following lines of code. Which of the two plots is saved as mpg-plot.png? Why?

my_bar_plot <- ggplot(mpg, aes(x = class)) +
  geom_bar()
my_scatter_plot <- ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point()
ggsave(filename = "mpg-plot.png", plot = my_bar_plot)
## Saving 7 x 5 in image

Because you have now assigned the plot to a name and used that name within the ggsave function. You are now being more specific.