This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(tidyverse)
filter(mpg, cty>=20)
## # A tibble: 56 × 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 manu… f 21 29 p comp…
## 2 audi a4 2 2008 4 manu… f 20 31 p comp…
## 3 audi a4 2 2008 4 auto… f 21 30 p comp…
## 4 audi a4 quattro 2 2008 4 manu… 4 20 28 p comp…
## 5 chevrolet malibu 2.4 2008 4 auto… f 22 30 r mids…
## 6 honda civic 1.6 1999 4 manu… f 28 33 r subc…
## 7 honda civic 1.6 1999 4 auto… f 24 32 r subc…
## 8 honda civic 1.6 1999 4 manu… f 25 32 r subc…
## 9 honda civic 1.6 1999 4 manu… f 23 29 p subc…
## 10 honda civic 1.6 1999 4 auto… f 24 32 r subc…
## # ℹ 46 more rows
mpg_eff <- filter(mpg, cty>=20)
view(mpg_eff)
mpg_ford <- filter(mpg,manufacturer =="ford" )
view(mpg_ford)
mpg_metric <- mutate(mpg, cty_metric = 0.42144 * cty)
glimpse(mpg_metric)
## Rows: 234
## Columns: 12
## $ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
## $ model <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
## $ displ <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
## $ year <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
## $ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
## $ trans <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
## $ drv <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
## $ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
## $ hwy <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
## $ fl <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
## $ class <chr> "compact", "compact", "compact", "compact", "compact", "c…
## $ cty_metric <dbl> 7.58592, 8.85024, 8.42880, 8.85024, 6.74304, 7.58592, 7.5…
#redoing the above function but with a pipe
mpg_metric <- mpg %>%
mutate(cty_metric = 0.42144 * cty)
view(mpg)
#takes mpg, groups it by class, then summarizes based on the mean
mpg %>%
group_by(class) %>%
summarise(mean(cty),
mean(hwy),
median(cty))
## # A tibble: 7 × 4
## class `mean(cty)` `mean(hwy)` `median(cty)`
## <chr> <dbl> <dbl> <dbl>
## 1 2seater 15.4 24.8 15
## 2 compact 20.1 28.3 20
## 3 midsize 18.8 27.3 18
## 4 minivan 15.8 22.4 16
## 5 pickup 13 16.9 13
## 6 subcompact 20.4 28.1 19
## 7 suv 13.5 18.1 13
ggplot(mpg, aes(x=cty))+
geom_histogram() +
labs (x="City Milage")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mpg, aes(x=cty))+
geom_freqpoly() +
labs (x="City Milage")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mpg, aes(x=cty))+
geom_histogram() +
geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
labs (x="City Milage")
## $x
## [1] "City Milage"
##
## attr(,"class")
## [1] "labels"
ggplot(mpg, aes(x=cty,
y=hwy))+
geom_point() +
geom_smooth (method="lm")
## `geom_smooth()` using formula = 'y ~ x'
ggplot(mpg, aes(x=cty,
y=hwy,
colour = class))+
geom_point()
ggplot(mpg, aes(x=cty,
y=hwy,
colour = class))+
geom_point() +
scale_color_brewer(palette = "Dark2")
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.