SELECT COLUMNS
# Keep only 2 columns
select(porgs, id, age)
# Drop the mass column
select(porgs, -mass)
# Put the age column first, but
# keep everything else the same
select(porgs, age, everything())
SORT ROWS
# Sort by age w/ YOUNGEST on top
arrange(porgs, age)
# Sort by age w/ ELDEST on top
arrange(porgs, desc(age))
# Sort by colors and then by age
arrange(porgs, color, desc(age))

DATES
Convert text to Date
Function Order of Input Output
mdy() Month-Day-Year :: 05-18-2019 2019-05-18
mdy_hm() Month-Day-Year Hour:Minutes :: 05-18-2019 8:35 2019-05-18 08:35:00 UTC
mdy_hms() Month-Day-Year Hour:Mins:Secs :: 05-18-2019 8:35:22 2019-05-18 08:35:22 UTC
Date parts
Function Date element
year() Year
month() Month as 1,2,3
day() Day of the month
wday() Day of the week
hour() Hour of the day (24hr)
tz() Time zone
JOIN TABLES

left_join() keeps all rows and columns in the left table, and joins rows in the right table with matching IDs.

# Table w/ porg ages and heights
porgs

# Table w/ porg names
porg_names

# Join together by id columns
together <- left_join(porgs, 
                      porg_names, 
                      by = "id")

SAVE DATA
Data tables
library(readr)
# Save data to a CSV text file
write_csv(porgs, "my_porg_data.csv")
Plots and images
library(ggsave) 
# Save the last plot you made
ggsave("most_recent_plot.png")
# Save earlier plot stored to variable
best_plot <- ggplot()
ggsave(best_plot, "best_plot.png")
HELP!
Online
From R
  • Go to: Help > Cheatsheets
  • Type ? in the Console
# Function help
?read_csv
# Search help
help.search("boxplot")
R COMMUNITY
SHORTCUTS
  • Run line: CTRL+ENTER
  • Save script: CTRL+S
  • Tidy code: highlight+CTRL+Shift+A
Find data


IFELSE: YES / NO DECISIONS

Use ifelse() to create new values that depend on the value of another column. For example, to only label the porgs with a height over 60 cm as “tall”.

# When a porg's height is > 60 cm label it as "tall", 
# otherwise label it as "short"
mutate(porgs, label = ifelse(height > 60, "tall", "short"))
R Support Crew

Barbara.Monaco

Kristie.Ellickson

Dorian.Kvale

Carl.Stenoien

Andrea.Borich

Derek.Nagel