Trasformare script in funzioni

Introduzione alla scrittura di funzioni in R

Richie Cotton

Data Evangelist at DataCamp

Un modello base di funzione

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

La firma

          function(arg1, arg2)

Il corpo

                               {
  # Do something
}
Introduzione alla scrittura di funzioni 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))
Introduzione alla scrittura di funzioni in R

Crea un modello

import_test_scores <- function() {






}
Introduzione alla scrittura di funzioni in R

Incolla lo script nel corpo

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))
}
Introduzione alla scrittura di funzioni in R

Scegli gli argomenti

import_test_scores <- function(filename) {         # <- solo 1 argomento
  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))
}
Introduzione alla scrittura di funzioni in R

Sostituisci valori specifici con argomenti

import_test_scores <- function(filename) {
  test_scores_geography_raw <- read_csv(filename)  # <- sostituisci il filename specifico

  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))
}
Introduzione alla scrittura di funzioni in R

Generalizza i nomi delle variabili

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)   # <- nomi variabili generalizzati

  test_scores_clean <- test_scores_raw %>%       # <- nomi variabili generalizzati
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introduzione alla scrittura di funzioni in R

Rimuovi l'assegnazione finale

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

  test_scores_raw %>%  # <- rimuovi l'assegnazione
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introduzione alla scrittura di funzioni in R

Usa la tua funzione

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")
Introduzione alla scrittura di funzioni in R

Argomenti di sample()

  • x: Un vettore di valori da cui campionare.
  • size: Quante volte vuoi campionare da x?
  • replace: Campionare con reinserimento o senza?
  • prob: Un vettore di pesi di campionamento per ciascun valore di x, che sommano a uno.
Introduzione alla scrittura di funzioni in R

Esercitiamoci!

Introduzione alla scrittura di funzioni in R

Preparing Video For Download...