Skip to content

Commit

Permalink
Updated lessons content
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjohnst86 committed Nov 2, 2015
1 parent 05f80b1 commit 08bd1a9
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 1,695 deletions.
4 changes: 2 additions & 2 deletions lessons/git/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ git config --list
## Generate these slides using (using R): ##


{% highlight r %}
```r
rmarkdown::render('slides.Rmd')
{% endhighlight %}
```
60 changes: 30 additions & 30 deletions lessons/r-wrangling/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ file formats.
> Example code:

{% highlight r %}
```r
## Export
write.csv(swiss, file = 'swiss.csv')

## Which is the same as:
write.table(swiss, file = 'swiss.csv', sep = ',')
{% endhighlight %}
```

## `read.csv` or `read.table` ##

Expand All @@ -54,10 +54,10 @@ write.table(swiss, file = 'swiss.csv', sep = ',')
> Example code:

{% highlight r %}
```r
write.csv(swiss, file = 'swiss.csv')
read.csv('swiss.csv')
{% endhighlight %}
```

## `head`, `names`, `str`, `summary` ##

Expand All @@ -71,13 +71,13 @@ median, frequency, and other basic statistics of each variable in the dataframe.
> Example code:

{% highlight r %}
```r
head(swiss)
names(swiss)
str(swiss)
summary(swiss)
class(swiss)
{% endhighlight %}
```

## `%>%` ##

Expand All @@ -90,7 +90,7 @@ do for some functions/commands (like `lm()`).
> Example code:

{% highlight r %}
```r
library(dplyr)
## This is the package that the pipe comes from
library(magrittr)
Expand All @@ -104,7 +104,7 @@ head(swiss)
swiss %>% head
swiss %>% head()
swiss %>% head(.)
{% endhighlight %}
```

## `tbl_df` ##

Expand All @@ -114,14 +114,14 @@ dataframe prettier.
> Example code:

{% highlight r %}
```r
library(dplyr)
## These are the same
tbl_df(ds)
ds %>% tbl_df
ds %>% tbl_df()
ds %>% tbl_df(.)
{% endhighlight %}
```

## `select` ##

Expand All @@ -131,7 +131,7 @@ variables based on pattern or if it contains some letter.
> Example code:

{% highlight r %}
```r
library(dplyr)
## These are the same
select(swiss, Education, Catholic, Fertility)
Expand All @@ -143,7 +143,7 @@ swiss %>% select(-Education, -Catholic)

## Select variables based on name or pattern
swiss %>% select(starts_with('E'), contains('Fert'), matches('mort'))
{% endhighlight %}
```

## `rename` ##

Expand All @@ -155,7 +155,7 @@ is, as it only renames.
> Example code:

{% highlight r %}
```r
library(dplyr)
## These are the same
rename(swiss, edu = Education)
Expand All @@ -167,7 +167,7 @@ swiss %>% rename(edu = Education, fert = Fertility)
## If you want to use select, but get the same functionality as rename, use the
## everything() function to select all other variables in the dataframe
swiss %>% select(edu = Education, everything())
{% endhighlight %}
```

## `filter` ##

Expand All @@ -178,7 +178,7 @@ greater than, `==` equals, `>=` or `<=` greater/less than or equal to.
> Example code:

{% highlight r %}
```r
library(dplyr)
## These are the same
filter(swiss, Catholic < 20, Examination == 15)
Expand All @@ -187,7 +187,7 @@ swiss %>% filter(., Catholic < 20, Examination == 15)

## For string/factor variables
swiss %>% filter(X == 'Aigle')
{% endhighlight %}
```

## `mutate` ##

Expand All @@ -197,7 +197,7 @@ that assigns a value based on the condition.
> Example code:

{% highlight r %}
```r
library(dplyr)
## These are the same
mutate(swiss, Infertile = ifelse(Fertility < 50, 'yes', 'no'))
Expand All @@ -206,7 +206,7 @@ swiss %>% mutate(., Infertile = ifelse(Fertility < 50, 'yes', 'no'))

## Or..
swiss %>% mutate(Test = 'yes', Number = 10)
{% endhighlight %}
```

## `arrange` ##

Expand All @@ -216,7 +216,7 @@ given (eg. `arrange(var1, var2)` sorts first by `var1` than by `var2`).
> Example code:

{% highlight r %}
```r
library(dplyr)
## These are the same
arrange(swiss, Education, Examination)
Expand All @@ -225,7 +225,7 @@ swiss %>% arrange(., Education, Examination)

## Or to do it descending
swiss %>% arrange(desc(Education))
{% endhighlight %}
```

## `group_by` ##

Expand All @@ -236,12 +236,12 @@ following commands based on the grouping.
> Example code:

{% highlight r %}
```r
library(dplyr)
swiss %>%
mutate(EarlyDeath = ifelse(Infant.Mortality >= 50, 'yes', 'no')) %>%
group_by(EarlyDeath)
{% endhighlight %}
```

## `summarise` ##

Expand All @@ -252,14 +252,14 @@ for sample size. This function is best used with `group_by()`.
> Example code:

{% highlight r %}
```r
library(dplyr)
swiss %>%
mutate(Educated = ifelse(Education >= 50, 'yes', 'no')) %>%
group_by(Educated) %>%
str()
summarise(mean = mean(Agriculture))
{% endhighlight %}
```


## `gather` ##
Expand All @@ -273,7 +273,7 @@ exclude (with a `-`) after the name of the two new variables.
> Example code:

{% highlight r %}
```r
library(dplyr)
library(tidyr)
## These are the same
Expand All @@ -286,7 +286,7 @@ swiss %>% add_rownames() %>% gather(Measure, Value, -rowname)

## Or include only some variables
swiss %>% gather(Measure, Value, Education, Fertility, Infant.Mortality)
{% endhighlight %}
```

## `spread` ##

Expand All @@ -296,19 +296,19 @@ dataframes.
> Example code:

{% highlight r %}
```r
library(dplyr)
library(tidyr)
swiss %>%
add_rownames() %>%
gather(Measure, Value, -rowname) %>%
spread(Measure, Value)
{% endhighlight %}
```

# Combined example using (almost) all functions:


{% highlight r %}
```r
swiss %>%
add_rownames() %>%
tbl_df() %>%
Expand All @@ -319,5 +319,5 @@ swiss %>%
group_by(Measure, Religious) %>%
summarise(mean = mean(Value)) %>%
spread(Measure, mean)
{% endhighlight %}
```

Loading

0 comments on commit 08bd1a9

Please sign in to comment.