将脚本改写为函数

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:是否放回抽样?
  • probx 每个取值的权重向量,总和为 1。
R 函数编写入门

Passons à la pratique !

R 函数编写入门

Preparing Video For Download...