Y kant I reed ur code?

Introduction to Writing Functions in R

Richie Cotton

Data Evangelist at DataCamp

dplyr verbs

select() selects columns

filter() filters rows

Introduction to Writing Functions in R

Function names should contain a verb

  • get
  • calculate (or maybe just calc)
  • run
  • process
  • import
  • clean
  • tidy
  • draw
Introduction to Writing Functions in R

lm() is badly named

  • Acronyms aren't self-explanatory
  • It doesn't contain a verb
  • There are lots of different linear models

A better name would be run_linear_regression()

Introduction to Writing Functions in R

Readability vs. typeability

  • Understanding code >> typing code
Introduction to Writing Functions in R

Readability vs. typeability

  • Understanding code >> typing code
  • Code editors have autocomplete

datacamp-autocomplete.png

Introduction to Writing Functions in R

Readability vs. typeability

  • Understanding code >> typing code
  • Code editors have autocomplete
  • You can alias common functions
h <- head
data(cats, package = "MASS")
h(cats)
  Sex Bwt Hwt
1   F 2.0 7.0
2   F 2.0 7.4
3   F 2.0 9.5
4   F 2.1 7.2
5   F 2.1 7.3
6   F 2.1 7.6
Introduction to Writing Functions in R

Arguments of lm()

args(lm)
function (formula, data, subset, weights, na.action, method = "qr", 
    model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
    contrasts = NULL, offset, ...)
Introduction to Writing Functions in R

Types of argument

  • Data arguments: what you compute on
  • Detail arguments: how you perform the computation
args(cor)
function (x, y = NULL, use = "everything", 
  method = c("pearson", "kendall", "spearman"))
Introduction to Writing Functions in R

Data args should precede detail args

This won't work

data %>%
  lm(formula)

because the data argument isn't first.

Introduction to Writing Functions in R

Our revised function for linear regression

run_linear_regression <- function(data, formula) {
  lm(formula, data)
}
cats %>% 
  run_linear_regression(Hwt ~ Bet + Sex)
Call:
lm(formula = formula, data = data)

Coefficients:
(Intercept)          Bwt         SexM  
    -0.4150       4.0758      -0.0821
Introduction to Writing Functions in R

Let's practice!

Introduction to Writing Functions in R

Preparing Video For Download...