การแปลงสคริปต์เป็นฟังก์ชัน

Introduction to Writing Functions in R

Richie Cotton

Data Evangelist at DataCamp

โครงสร้างพื้นฐานของฟังก์ชัน

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

Signature

          function(arg1, arg2)

Body

                               {
  # Do something
}
Introduction to Writing Functions 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))
Introduction to Writing Functions in R

สร้างโครงร่างฟังก์ชัน

import_test_scores <- function() {






}
Introduction to Writing Functions in R

วางสคริปต์ลงใน 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))
}
Introduction to Writing Functions in R

กำหนด argument ของฟังก์ชัน

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))
}
Introduction to Writing Functions in R

แทนที่ค่าเฉพาะเจาะจงด้วย argument

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))
}
Introduction to Writing Functions in R

ทำให้ชื่อตัวแปรเป็นแบบทั่วไป

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))
}
Introduction to Writing Functions in R

ลบการกำหนดค่าสุดท้ายออก

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))
}
Introduction to Writing Functions in R

เรียกใช้ฟังก์ชันที่สร้างขึ้น

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")
Introduction to Writing Functions in R

argument ของ sample()

  • x: เวกเตอร์ของค่าที่ต้องการสุ่ม
  • size: ต้องการสุ่มจาก x กี่ครั้ง?
  • replace: สุ่มแบบมีการคืนค่าหรือไม่?
  • prob: เวกเตอร์น้ำหนักการสุ่มสำหรับแต่ละค่าใน x รวมกันเท่ากับ 1
Introduction to Writing Functions in R

มาฝึกกันเถอะ!

Introduction to Writing Functions in R

Preparing Video For Download...