-
Notifications
You must be signed in to change notification settings - Fork 1
/
lessons_writing_functions.Rmd
108 lines (83 loc) · 3.51 KB
/
lessons_writing_functions.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: "Writing Functions"
---
## Learning Objectives
- Write R functions to make your code less redundant and more efficient
## Required Packages and Datasets
If you dont' have the `uwpols501` package yet, install it first from GitHub.
```{r, eval = FALSE}
library(devtools)
install_github("UW-POLS501/r-uwpols501")
```
Make sure you can load all these packages and dataset before starting the module
```{r, message = FALSE}
library(dplyr)
library(uwpols501)
data(turnout)
data(iver)
```
## Intro
Why should we use functions? Well, don't take it from me, take it from [Grolemund & Wickham (2016)](http://r4ds.had.co.nz/functions.html):
*Writing a function has three big advantages over using copy-and-paste:*
1. *You drastically reduce the chances of making incidental mistakes when you copy and paste.*
2. *As requirements change, you only need to update code in one place, instead of many.*
3. *You can give a function an evocative name that makes your code easier to understand.*
Writing a function step by step:
1. Specify a function name
2. Use the `function()` command
3. Specify inside the `function()` command the arguments that the functions takes
4. Write what the function does between the curly brackets `{ }`
(Function names should be verbs, and arguments should be nouns)
A function has the following skeleton:
```{r, eval = FALSE}
function_name <- function(argument1, argument2, ...) {
# do_something with the arguments
output <- argument1 + argument2
return(output)
}
```
A short/silly example:
```{r}
say_hi <- function(name) {
full_statement <- paste0("Hi! My name is ", name)
return(full_statement)
}
full_statement <- say_hi("Andreu")
full_statement
```
## Real Example (1)
For the final project last quarter, one of your classmates needed to retrieve the first digit of all numbers for several numeric variables. So the person had to write the same code multiple times. Let's now write a function that would simplify that code.
```{r}
get_first_digit <- function(variable) {
num_digits <- nchar(variable)
var_first_num <- variable %/% (10 ^ (nchar(variable) - 1))
return(var_first_num)
}
```
Now apply the function `get_first_digit()` that we just created to the variables `age` and `educate` of the `turnout` dataset.
```{r}
turnout$age_new <- get_first_digit(turnout$age)
turnout$educate_new <- get_first_digit(turnout$educate)
```
## Real Example (2)
Last quarter some other classmates needed to create a dummy variable indicating if the country or political party for any given row was part of a particular list of country or political party.
Here we first create a list of countries we are interested in, and then we write a function that takes a `country` variable as an argument, and returns a dummy variable indicating which observations are in the list of countries of interest.
```{r}
peripherial_countries <- c("Portugal", "Italy", "Ireland",
"Cyprus", "Greece", "Spain")
create_per_dummy <- function(country_variable) {
country_variable <- as.character(country_variable)
dummy_boolean <- country_variable %in% peripherial_countries
dummy_numeric <- as.numeric(dummy_boolean)
return(dummy_numeric)
}
```
Now we apply the function to the `cty` variable of the `iver` dataset from the `uwpols501` package
```{r}
head(iver, n = 10)
iver$peripherial_cty <- create_per_dummy(iver$cty)
head(iver, n = 10)
```
`r challenge_start()`
Look through some of your previous code from other classes and projects, and write a function to reduce redundancy (10 min.)
`r challenge_end()`