Convertendo scripts em funções

Introdução à Escrita de Funções em R

Richie Cotton

Data Evangelist at DataCamp

Um modelo básico de função

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

A assinatura

          function(arg1, arg2)

O corpo

                               {
  # Do something
}
Introdução à Escrita de Funções em 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))
Introdução à Escrita de Funções em R

Crie um modelo

import_test_scores <- function() {






}
Introdução à Escrita de Funções em R

Cole seu script no 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))
}
Introdução à Escrita de Funções em R

Escolha os argumentos

import_test_scores <- function(filename) {         # <- apenas 1 argumento
  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))
}
Introdução à Escrita de Funções em R

Troque valores específicos por argumentos

import_test_scores <- function(filename) {
  test_scores_geography_raw <- read_csv(filename)  # <- substituir nome de arquivo específico

  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))
}
Introdução à Escrita de Funções em R

Generalize os nomes das variáveis

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)   # <- nomes de variáveis generalizados

  test_scores_clean <- test_scores_raw %>%       # <- nomes de variáveis generalizados
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introdução à Escrita de Funções em R

Remova a atribuição final

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

  test_scores_raw %>%  # <- remover atribuição
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introdução à Escrita de Funções em R

Use sua função

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")
Introdução à Escrita de Funções em R

Argumentos de sample()

  • x: Um vetor de valores para amostrar.
  • size: Quantas vezes você quer amostrar de x?
  • replace: Amostrar com reposição?
  • prob: Um vetor de pesos de amostragem para cada valor de x, somando um.
Introdução à Escrita de Funções em R

Vamos praticar!

Introdução à Escrita de Funções em R

Preparing Video For Download...