Conversia scripturilor în funcții

Introducere în scrierea funcțiilor în R

Richie Cotton

Data Evangelist at DataCamp

Un șablon de funcție de bază

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

Semnătura

          function(arg1, arg2)

Corpul

                               {
  # Do something
}
Introducere în scrierea funcțiilor în 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))
Introducere în scrierea funcțiilor în R

Creați un șablon

import_test_scores <- function() {






}
Introducere în scrierea funcțiilor în R

Inserați scriptul în corp

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))
}
Introducere în scrierea funcțiilor în R

Alegeți argumentele

import_test_scores <- function(filename) {         # <- only 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))
}
Introducere în scrierea funcțiilor în R

Înlocuiți valorile specifice cu argumente

import_test_scores <- function(filename) {
  test_scores_geography_raw <- read_csv(filename)  # <- replace specific filename

  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))
}
Introducere în scrierea funcțiilor în R

Generalizați numele variabilelor

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)   # <- variable names generalized

  test_scores_clean <- test_scores_raw %>%       # <- variable names generalized
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introducere în scrierea funcțiilor în R

Eliminați atribuirea finală

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

  test_scores_raw %>%  # <- remove assignment
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Introducere în scrierea funcțiilor în R

Utilizați funcția

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")
Introducere în scrierea funcțiilor în R

Argumentele funcției sample()

  • x: Un vector de valori din care se eșantionează.
  • size: De câte ori doriți să eșantionați din x?
  • replace: Eșantionați cu sau fără înlocuire?
  • prob: Un vector de ponderi pentru fiecare valoare din x, cu suma egală cu unu.
Introducere în scrierea funcțiilor în R

Să exersăm!

Introducere în scrierea funcțiilor în R

Preparing Video For Download...