スクリプトを関数に変換する

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) {         # <- 引数は1つのみ
  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)  # <- 固有のファイル名を置換

  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)   # <- 変数名を一般化

  test_scores_clean <- test_scores_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_raw <- read_csv(filename)

  test_scores_raw %>%  # <- 代入をやめる
    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...