Wed Oct 19, 2016

Last Time: 5MV

  1. select() columns by variable name: front of cheatsheet, bottom right
  2. filter() rows matching criteria: front of cheatsheet, bottom middle.
  3. summarise() numerical variables that are group_by() categorical variables
  4. mutate() existing variables to create new ones
  5. arrange() rows

And piping %>% i.e. then

Keyboard Shortcut

Everyone place your cursor in the console and type:

  • Mac users: COMMAND+SHIFT+M
  • Windows uers: CTRL+SHIFT+M

Today: 5MV

  1. select() columns by variable name: front of cheatsheet, bottom right
  2. filter() rows matching criteria: front of cheatsheet, bottom middle
  3. summarise() numerical variables that are group_by() categorical variables
  4. mutate() existing variables to create new ones: back of cheatsheet, center column However in this class you probably won't use these often.
  5. arrange() rows

mutate()

Always of the form

mutate(DATASET_NAME, NEW_VARIABLE_NAME = OLD_VARIABLE_NAMES)

or if using piping %>% (note no comma)

DATASET_NAME %>% mutate(NEW_VARIABLE_NAME = OLD_VARIABLE_NAMES)

Example

Load the following in your console

library(dplyr)

# Create data frame with two variables
test_data <- data_frame(
  name=c("Kimmy", "Kimmy", "Kimmy", "Titus", "Titus"),
  value=c(1, 2, 3, 4, 5)
)

# See contents in console
test_data

Mutate

Test out the following bits individually in your console:

# Bit1: Mutate the variable value by adding 200, overwrite value
mutate(test_data, value = value+200)

# Bit2: Same thing, but with piping. Note there is no comma now 
# as the %>% by default # pipes to the first spot
test_data %>% mutate(value = value+200)

# Bit3: Mutate the variable value by adding 200, but now save
# in a new variable called new_value
test_data %>% mutate(new_value = value+200)