tidyversemagrittrinstall.packages(c("tidyverse", "magrittr"))library(tidyverse)## Warning: package 'tidyverse' was built under R version 3.4.1
## Warning: Installed Rcpp (0.12.13) different from Rcpp used to build dplyr (0.12.11).
## Please reinstall dplyr to avoid random crashes or undefined behavior.
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'tidyr' was built under R version 3.4.1
## Warning: package 'readr' was built under R version 3.4.1
## Warning: package 'purrr' was built under R version 3.4.1
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(magrittr)##
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
##
## set_names
## The following object is masked from 'package:tidyr':
##
## extract
tidyverse# Base R
dat <- read.csv('example.csv')
# Tidyverse
dat <- read_csv('example.csv')data()starwars datasetstarwars## # A tibble: 87 x 13
## name height mass hair_color skin_color eye_color
## <chr> <int> <dbl> <chr> <chr> <chr>
## 1 Luke Skywalker 172 77 blond fair blue
## 2 C-3PO 167 75 <NA> gold yellow
## 3 R2-D2 96 32 <NA> white, blue red
## 4 Darth Vader 202 136 none white yellow
## 5 Leia Organa 150 49 brown light brown
## 6 Owen Lars 178 120 brown, grey light blue
## 7 Beru Whitesun lars 165 75 brown light blue
## 8 R5-D4 97 32 <NA> white, red red
## 9 Biggs Darklighter 183 84 black light brown
## 10 Obi-Wan Kenobi 182 77 auburn, white fair blue-gray
## # ... with 77 more rows, and 7 more variables: birth_year <dbl>,
## # gender <chr>, homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
> starwars
> print(starwars)
> str(starwars)
> head(starwars) # first 5 rows
summary(starwars) ## name height mass homeworld
## Length:87 Min. : 66.0 Min. : 15.00 Naboo :11
## Class :character 1st Qu.:167.0 1st Qu.: 55.60 Tatooine :10
## Mode :character Median :180.0 Median : 79.00 Alderaan : 3
## Mean :174.4 Mean : 97.31 Coruscant: 3
## 3rd Qu.:191.0 3rd Qu.: 84.50 Kamino : 3
## Max. :264.0 Max. :1358.00 (Other) :47
## NA's :6 NA's :28 NA's :10
Click on the variable name in the Environments pane
Will execute this code:
View(mtcars)%>%Sends the output of the LHS function to the first argument of the RHS function
sum(1:8) %>%
sqrt()## [1] 6
tidy-nesstable %>%
gather(`1999`, `2000`, key = "year", value = "cases")starwars %>% filter(height < 90)## # A tibble: 3 x 13
## name height mass hair_color skin_color eye_color
## <chr> <int> <dbl> <chr> <chr> <chr>
## 1 Yoda 66 17 white green brown
## 2 Wicket Systri Warrick 88 20 brown brown brown
## 3 Ratts Tyerell 79 15 none grey, blue unknown
## # ... with 7 more variables: birth_year <dbl>, gender <chr>,
## # homeworld <fctr>, species <chr>, films <list>, vehicles <list>,
## # starships <list>
starwars %>%
filter(height < 90) %>%
select(name, gender, species, height)## # A tibble: 3 x 4
## name gender species height
## <chr> <chr> <chr> <int>
## 1 Yoda male Yoda's species 66
## 2 Wicket Systri Warrick male Ewok 88
## 3 Ratts Tyerell male Aleena 79
starwars %>% filter(eye_color == "red") %>% select(name)## # A tibble: 5 x 1
## name
## <chr>
## 1 R2-D2
## 2 R5-D4
## 3 IG-88
## 4 Bossk
## 5 Nute Gunray
filter(starwars, height < 90) %>%
select(name, gender, species, height) %>%
arrange(height)## # A tibble: 3 x 4
## name gender species height
## <chr> <chr> <chr> <int>
## 1 Yoda male Yoda's species 66
## 2 Ratts Tyerell male Aleena 79
## 3 Wicket Systri Warrick male Ewok 88
starwars %>% arrange(name)## # A tibble: 87 x 13
## name height mass hair_color skin_color
## <chr> <int> <dbl> <chr> <chr>
## 1 Ackbar 180 83 none brown mottle
## 2 Adi Gallia 184 50 none dark
## 3 Anakin Skywalker 188 84 blond fair
## 4 Arvel Crynyd NA NA brown fair
## 5 Ayla Secura 178 55 none blue
## 6 Bail Prestor Organa 191 NA black tan
## 7 Barriss Offee 166 50 black yellow
## 8 BB8 NA NA none none
## 9 Ben Quadinaros 163 65 none grey, green, yellow
## 10 Beru Whitesun lars 165 75 brown light
## # ... with 77 more rows, and 8 more variables: eye_color <chr>,
## # birth_year <dbl>, gender <chr>, homeworld <fctr>, species <chr>,
## # films <list>, vehicles <list>, starships <list>
starwars %>%
na.omit() %>%
group_by(species) %>%
summarize(avg_mass = mean(mass))## # A tibble: 11 x 2
## species avg_mass
## <chr> <dbl>
## 1 Cerean 82.00000
## 2 Ewok 20.00000
## 3 Gungan 66.00000
## 4 Human 81.01111
## 5 Kel Dor 80.00000
## 6 Mirialan 53.10000
## 7 Mon Calamari 83.00000
## 8 Trandoshan 113.00000
## 9 Twi'lek 55.00000
## 10 Wookiee 112.00000
## 11 Zabrak 80.00000
starwars %>%
na.omit() %>%
group_by(species, gender) %>%
select(name, gender, species, mass) %>%
mutate(avg_mass = mean(mass))## # A tibble: 29 x 5
## # Groups: species, gender [12]
## name gender species mass avg_mass
## <chr> <chr> <chr> <dbl> <dbl>
## 1 Luke Skywalker male Human 77 85.94667
## 2 Darth Vader male Human 136 85.94667
## 3 Leia Organa female Human 49 56.33333
## 4 Owen Lars male Human 120 85.94667
## 5 Beru Whitesun lars female Human 75 56.33333
## 6 Biggs Darklighter male Human 84 85.94667
## 7 Obi-Wan Kenobi male Human 77 85.94667
## 8 Anakin Skywalker male Human 84 85.94667
## 9 Chewbacca male Wookiee 112 112.00000
## 10 Han Solo male Human 80 85.94667
## # ... with 19 more rows
starwars %>%
mutate(height_plus_mass = height + mass) %>%
select(name, height, mass, height_plus_mass)## # A tibble: 87 x 4
## name height mass height_plus_mass
## <chr> <int> <dbl> <dbl>
## 1 Luke Skywalker 172 77 249
## 2 C-3PO 167 75 242
## 3 R2-D2 96 32 128
## 4 Darth Vader 202 136 338
## 5 Leia Organa 150 49 199
## 6 Owen Lars 178 120 298
## 7 Beru Whitesun lars 165 75 240
## 8 R5-D4 97 32 129
## 9 Biggs Darklighter 183 84 267
## 10 Obi-Wan Kenobi 182 77 259
## # ... with 77 more rows
| Factor | Scalar/Numeric |
|---|---|
| discrete | continuous |
| Example: Human, Droid, Wookie |
Example: 0.7, 1.2, 3.4 |
nums <- c(0.7, 1.2, 3.4)
factor_nums <- as.factor(nums)
levels(factor_nums)## [1] "0.7" "1.2" "3.4"
nums + 1## [1] 1.7 2.2 4.4
factor_nums + 1## Warning in Ops.factor(factor_nums, 1): '+' not meaningful for factors
## [1] NA NA NA
ggplot# Base R
plot(starwars$height, type='p', col='red', pch=16)ggplot functionggplot(data=starwars, aes(x=height, y=mass)) + ...... + geom_point(), + geom_boxplot(), etc.ggplot(starwars, aes(x=height, y=mass)) +
geom_point(aes(color=gender), size=5) ggplot(subset(starwars, species %in% c('Droid', 'Human', "Gungan")),
aes(x=species, y=height)) +
geom_boxplot()ggplot(starwars, aes(x=height, y=mass)) +
geom_point(size=5) +
stat_smooth(method='lm')dplyr to ggplotstarwars %>%
filter(species == 'Human' & gender %in% c('male', 'female')) %>%
ggplot(aes(mass)) +
geom_histogram() +
facet_grid(. ~ gender)geom_bar to plot the count of different species with a home world of Naboo or Tatooinestarwars %>%
filter(homeworld %in% c("Naboo", "Tatooine")) %>%
ggplot(aes(species)) +
geom_bar() +
facet_grid(. ~ homeworld)%>%) can link many functions