การส่งอาร์กิวเมนต์ระหว่างฟังก์ชัน

Introduction to Writing Functions in R

Richie Cotton

Data Evangelist at DataCamp

การคำนวณค่าเฉลี่ยเรขาคณิต

x %>%
  log() %>%
  mean() %>%
  exp()
Introduction to Writing Functions in R

นำมาห่อด้วยฟังก์ชัน

calc_geometric_mean <- function(x) {
  x %>%
    log() %>%
    mean() %>%
    exp()
}
Introduction to Writing Functions in R

การจัดการค่าที่หายไป

calc_geometric_mean <- function(x, na.rm = FALSE) {
  x %>%
    log() %>%
    mean(na.rm = na.rm) %>%
    exp()
}
Introduction to Writing Functions in R

การใช้ ...

calc_geometric_mean <- function(x, ...) {
  x %>%
    log() %>%
    mean(...) %>%
    exp()
}
Introduction to Writing Functions in R

การแลกเปลี่ยน

ข้อดี

  • พิมพ์โค้ดน้อยลง
  • ไม่ต้องจับคู่ signature

ข้อเสีย

  • ต้องไว้วางใจฟังก์ชันภายใน
  • อินเทอร์เฟซไม่ชัดเจนสำหรับผู้ใช้
Introduction to Writing Functions in R

มาฝึกกันเถอะ!

Introduction to Writing Functions in R

Preparing Video For Download...