Introduction to Writing Functions in R
Richie Cotton
Data Evangelist at DataCamp
Mean has 3 arguments
x
: A numeric or date-time vector.trim
: The proportion of outliers from each end to remove before calculatingna.rm
: Remove before calculatingPass arguments by position
mean(numbers, 0.1, TRUE)
Pass arguments by name
mean(na.rm = TRUE, trim = 0.1, x = numbers)
Common arguments by position, rare arguments by name
mean(numbers, trim = 0.1, na.rm = TRUE)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_english_raw <- read_csv("test_scores_english.csv")
library(dplyr)
test_scores_english_clean <- test_scores_english_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_art_raw <- read_csv("test_scores_art.csv")
library(dplyr)
test_scores_art_clean <- test_scores_art_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_spanish_raw <- read_csv("test_scores_spanish.csv")
library(dplyr)
test_scores_spanish_clean <- test_scores_spanish_raw %>%
select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
library(lubridate)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date))
library(readr)
test_scores_english_raw <- read_csv("test_scores_english.csv")
library(dplyr)
library(lubridate)
test_scores_english_clean <- test_scores_english_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date))
library(readr)
test_scores_art_raw <- read_csv("test_scores_art.csv")
library(dplyr)
library(lubridate)
test_scores_art_clean <- test_scores_art_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date))
library(readr)
test_scores_spanish_raw <- read_csv("test_scores_spanish.csv")
library(dplyr)
library(lubridate)
test_scores_spanish_clean <- test_scores_spanish_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date))
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
library(lubridate)
test_scores_geography_clean <- test_scores_geography_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date)) %>%
filter(!is.na(score))
library(readr)
test_scores_english_raw <- read_csv("test_scores_english.csv")
library(dplyr)
library(lubridate)
test_scores_english_clean <- test_scores_english_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date)) %>%
filter(!is.na(score))
library(readr)
test_scores_art_raw <- read_csv("test_scores_art.csv")
library(dplyr)
library(lubridate)
test_scores_art_clean <- test_scores_art_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date)) %>%
filter(is.na(score))
library(readr)
test_scores_spanish_raw <- read_csv("test_scores_spanish.csv")
library(dplyr)
library(lubridate)
test_scores_spanish_clean <- test_scores_spanish_raw %>%
select(person_id, first_name, last_name, test_date, score) %>%
mutate(test_date = mdy(test_date)) %>%
filter(!is.na(score))
Functions eliminate repetition from your code, which
Functions also allow code reuse and sharing.
Introduction to Writing Functions in R