为什么要使用函数

R 函数编写入门

Richie Cotton

Data Evangelist at DataCamp

mean() 的参数

mean 有 3 个参数

  • x:数值或日期时间向量。
  • trim:在计算前从两端各移除的比例。
  • na.rm:在计算前移除缺失值。
R 函数编写入门

调用 mean()

按位置传参

mean(numbers, 0.1, TRUE)

按名称传参

mean(na.rm = TRUE, trim = 0.1, x = numbers)

常用参数用位置,少用参数用名称

mean(numbers, trim = 0.1, na.rm = TRUE)

R 函数编写入门

分析测试分数

library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")
library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
R 函数编写入门
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")

library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")

library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")

library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")

library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
R 函数编写入门
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")

library(dplyr)
test_scores_geography_clean <- test_scores_geography_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_english_raw <- read_csv("test_scores_english.csv")

library(dplyr)
test_scores_english_clean <- test_scores_english_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_art_raw <- read_csv("test_scores_art.csv")

library(dplyr)
test_scores_art_clean <- test_scores_art_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
library(readr)
test_scores_spanish_raw <- read_csv("test_scores_spanish.csv")

library(dplyr)
test_scores_spanish_clean <- test_scores_spanish_raw %>% 
  select(person_id, first_name, last_name, test_date, score)
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))
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))
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))
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))
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 函数编写入门

编写函数的好处

函数能消除重复代码,可

  • 减少工作量,
  • 避免错误。

函数也便于复用与共享。

R 函数编写入门

Passons à la pratique !

R 函数编写入门

Preparing Video For Download...