Writing Functions in R
CVC 2016
require(mosaic)
require(babynames)babynames %>%
filter(name %in% c("Mary", "Jane"), sex == "F") %>%
ggplot() +
geom_line(aes(x = year, y = prop, color = name))We would like to generalize our code to handle any set of names we want to compare. In R we do this by writing functions.
We begin with our two questions:
What do we want R to do?
NameTrendPlot() might be a good name for the functionWhat does it need to know to do this?
The general form of a fucntion definition is
functionName <- function( arg1 = default1, arg2 = default2, ... ) {
# do stuff here
# result of last line is returned.
# Can also use explicit return().
}NameTrend <- function(names, gender) {
babynames %>%
filter(name %in% names, sex == gender) %>%
ggplot() +
geom_line(aes(x = year, y = prop, color = name))
}NameTrend(c("Tom", "Dick", "Joe"), gender = "M")We can arguments to control more features of the plot.
NameTrend2 <- function(names, gender = c("M", "F"), linewidth = 1) {
babynames %>%
filter(name %in% names, sex %in% gender) %>%
ggplot() +
geom_line(aes(x = year, y = prop, color = name, linetype = sex),
size = linewidth)
}NameTrend2( c("Tom", "Dick", "Harry"), gender = "M", linewidth = 3)NameTrend2( c("Hillary", "Beverly"), linewidth = 1)