Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active April 9, 2017 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/59aca1b853a5bd7a01bfac704f756db7 to your computer and use it in GitHub Desktop.
Save ryanburge/59aca1b853a5bd7a01bfac704f756db7 to your computer and use it in GitHub Desktop.
Class Instructions for 3/22/2017 - dplyr practice
## RUN ALL THIS SYNTAX BEFORE WE START
## This will install and load all the packages you need for class today.
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
packages <- c("ggplot2", "dplyr", "car", "plotly")
ipak(packages)
## STOP AND WAIT FOR ME
salary <- data.frame("name" =c("Bill Gates", "Warren Buffett", "Donald Trump", "Bob", "Steve", "Lisa", "Karen", "David Glassman", "Ben", "Piper", "Hope"),
money = c(10000000,12000000,2000000, 42000,35000,54000,27500,400000,33000, 75000, 87000))
salary$gender <- c("M", "M", "M", "M", "M", "F", "F", "M", "M", "F", "F")
## Let's do that group_by command exercise again. It should work this time.
## Now, I've found a basic dataset of baseball players
bball <- read.csv(url("https://raw.githubusercontent.com/ryanburge/pls2003_sp17/master/dplyr.csv"))
## Some sample commands
group_by() - What variable to you want to group your analysis by?
arrange() - Arrange things in an order if you want to do it reverse it: arrange(dataset, desc(variable))
filter() - You can use == or <= or !=. If it's a word remember to surround it by quotation marks
summarise() - You are creating a new dataset, usually used with group_by(). summarise(avg = mean(variable))
## Create a dataset of just the American League
## Overwrite that dataset: Make it American Leaguers since 2000
## Who is the highest paid ball player in that dataset you just created?
## Now, do it all in one line.
## What individual had the most ABs in one year?
## What individual had the most strikeouts in one year?
## How has home run hitting changed over time in MLB?
## How has salaries changed over time?
## Plot it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment