將腳本轉成函式

R 函式撰寫入門

Richie Cotton

Data Evangelist at DataCamp

基本函式樣板

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

簽名

          function(arg1, arg2)

主體

                               {
  # Do something
}
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))
R 函式撰寫入門

建立樣板

import_test_scores <- function() {






}
R 函式撰寫入門

把腳本貼進主體

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))
}
R 函式撰寫入門

選定參數

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))
}
R 函式撰寫入門

以參數取代特定值

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))
}
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))
}
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))
}
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")
R 函式撰寫入門

sample() 的參數

  • x:要抽樣的向量。
  • size:你想從 x 抽樣幾次?
  • replace:是否放回抽樣?
  • prob:對應 x 每個值的抽樣權重向量,總和為 1。
R 函式撰寫入門

一起來練習吧!

R 函式撰寫入門

Preparing Video For Download...