Scripts omzetten naar functies

Introductie tot functies schrijven in R

Richie Cotton

Data Evangelist at DataCamp

Een basis-sjabloon voor functies

my_fun <- function(arg1, arg2) {
  # Do something
}

De signature

          function(arg1, arg2)

De body

                               {
  # Do something
}
Introductie tot functies schrijven in R
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))
Introductie tot functies schrijven in R

Maak een sjabloon

import_test_scores <- function() {






}
Introductie tot functies schrijven in R

Plak je script in de body

import_test_scores <- function() {
  test_scores_geography_raw <- read_csv("test_scores_geography.csv")

  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))
}
Introductie tot functies schrijven in R

Kies de argumenten

import_test_scores <- function(filename) {         # <- slechts 1 argument
  test_scores_geography_raw <- read_csv("test_scores_geography.csv")

  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))
}
Introductie tot functies schrijven in R

Vervang specifieke waarden door argumenten

import_test_scores <- function(filename) {
  test_scores_geography_raw <- read_csv(filename)  # <- specifieke bestandsnaam vervangen

  test_scores_geography_clean <- raw_data %>% 
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introductie tot functies schrijven in R

Maak variabelenamen generiek

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)   # <- variabelenamen generiek gemaakt

  test_scores_clean <- test_scores_raw %>%       # <- variabelenamen generiek gemaakt
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introductie tot functies schrijven in R

Verwijder de laatste toekenning

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)

  test_scores_raw %>%  # <- toekenning verwijderen
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introductie tot functies schrijven in R

Gebruik je functie

test_scores_geography <- import_test_scores("test_scores_geography.csv")
test_scores_english <- import_test_scores("test_scores_english.csv")
test_scores_art <- import_test_scores("test_scores_art.csv")
test_scores_spanish <- import_test_scores("test_scores_spanish.csv")
Introductie tot functies schrijven in R

Argumenten van sample()

  • x: Een vector met waarden om uit te sampelen.
  • size: Hoe vaak wil je uit x sampelen?
  • replace: Met terugleggen sampelen of niet?
  • prob: Een vector met wegingen per waarde in x, som is 1.
Introductie tot functies schrijven in R

Laten we oefenen!

Introductie tot functies schrijven in R

Preparing Video For Download...