指定類別

在 R 中使用 S3 與 R6 的物件導向程式設計

Richie Cotton

Data Evangelist at DataCamp

(x <- rexp(10))
0.195051 2.191040 0.498703 0.976122 0.299001
0.105187 0.090073 2.328233 3.043201 2.129631
class(x) <- "random_numbers"
x
0.195051 2.191040 0.498703 0.976122 0.299001
0.105187 0.090073 2.328233 3.043201 2.129631
attr(,"class")
"random_numbers"
在 R 中使用 S3 與 R6 的物件導向程式設計
class(x)
"random_numbers"
typeof(x)
"double"
在 R 中使用 S3 與 R6 的物件導向程式設計
is.numeric(x)
TRUE
length(x)
10
mean(x)
1.1856
在 R 中使用 S3 與 R6 的物件導向程式設計

重點整理

  • 你可以「覆寫」class()
  • 這「不會」破壞現有功能
在 R 中使用 S3 與 R6 的物件導向程式設計

一起來練習吧!

在 R 中使用 S3 與 R6 的物件導向程式設計

Preparing Video For Download...