Chuyển script thành hàm

Nhập môn viết hàm trong R

Richie Cotton

Data Evangelist at DataCamp

Mẫu hàm cơ bản

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

Chữ ký

          function(arg1, arg2)

Thân hàm

                               {
  # Do something
}
Nhập môn viết hàm trong 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))
Nhập môn viết hàm trong R

Tạo mẫu khung

import_test_scores <- function() {






}
Nhập môn viết hàm trong R

Dán script vào thân hàm

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))
}
Nhập môn viết hàm trong R

Chọn đối số

import_test_scores <- function(filename) {         # <- chỉ 1 đối số
  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))
}
Nhập môn viết hàm trong R

Thay giá trị cụ thể bằng đối số

import_test_scores <- function(filename) {
  test_scores_geography_raw <- read_csv(filename)  # <- thay tên tệp cụ thể

  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))
}
Nhập môn viết hàm trong R

Tổng quát hóa tên biến

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)   # <- tổng quát tên biến

  test_scores_clean <- test_scores_raw %>%       # <- tổng quát tên biến
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Nhập môn viết hàm trong R

Bỏ phép gán cuối cùng

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

  test_scores_raw %>%  # <- bỏ gán cuối
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
Nhập môn viết hàm trong R

Dùng hàm của bạn

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")
Nhập môn viết hàm trong R

Đối số của sample()

  • x: Vector giá trị để lấy mẫu.
  • size: Lấy mẫu từ x bao nhiêu lần?
  • replace: Lấy mẫu có hoàn lại hay không?
  • prob: Vector trọng số cho từng giá trị của x, tổng bằng 1.
Nhập môn viết hàm trong R

Ayo berlatih!

Nhập môn viết hàm trong R

Preparing Video For Download...