メソッド、属性、総称的関数
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
COLOR(magenta){SIZE(15){オブジェクトの属性とクラス}}
// 間瀬 2004.04.28
R のオブジェクトにはCOLOR(blue){属性(attribute)}と呼ばれ...
> (x <- 1:8) # 等差数列ベクトル
[1] 1 2 3 4 5 6 7 8
> dim(x) <- c(2,4) # x の次元属性を c(2,4) ...
> x # 2x4 行列になる
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> dim(x) <- c(2,2,2) # x の次元属性を c(2,2,2)...
> x # 3次元 (2x2x2) 配列になる
, , 1 # x[,,1] 部分の行列
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2 # x[,,2] 部分の行列
[,1] [,2]
[1,] 5 7
[2,] 6 8
> x[7] # しかし依然としてベクト...
[1] 7
属性は隠された情報であり、COLOR(red){attr()} 関数や COLOR...
> x <- lm(1:5 ~ rnorm(5)) # 例示用の簡単な...
> comment(x) <- "Example (2004.4.19)" # x にコメント属...
> x # 結果の出力(属性...
Call:
lm(formula = 1:5 ~ rnorm(5))
Coefficients:
(Intercept) rnorm(5)
2.7041 -0.5122
> attributes(x) # オブジェクトの...
$names # names(名前)属性...
[1] "coefficients" "residuals" "effects" "rank"
[5] "fitted.values" "assign" "qr" "df.residual"
[9] "xlevels" "call" "terms" "model"
$class # クラス属性
[1] "lm"
$comment
[1] "Example (2004.4.19)" # コメント属性の...
> str(x) # str() 関数はオブジェクトの持つ全情報を簡...
List of 12
$ coefficients : Named num [1:2] 2.704 -0.512
..- attr(*, "names")= chr [1:2] "(Intercept)" "rnorm(5)"
(途中略)
- attr(*, "class")= chr "lm"
R 言語で頻繁に使われる COLOR(red){plot()} 等の関数はCOLOR...
> methods(plot) # 総称的関数 plot() に対す...
[1] plot.HoltWinters* plot.POSIXct plot.POSIXlt
[4] plot.TukeyHSD plot.acf* plot.data.frame
(途中省略)
[28] plot.table plot.ts plot.tskernel*
Non-visible functions are asterisked
> methods(class = "lm") # クラス "lm" のオブジェク...
[1] add1.lm alias.lm anova.lm case.names.lm
[5] coef.lm confint.lm cooks.distance.lm deviance.lm
(途中省略)
[33] variable.names.lm vcov.lm
注意:もし自前のクラスとそのメソッドを定義したければ、概...
(1) クラス名 "hoge" を定義。~
(2) 関数 Hoge() の返り値にクラス属性 "hoge" を与え、さら...
(3) クラス ”hoge" 用の plot メソッド関数 plot.hoge() を定...
(4) R にはこれらを以上の処理を実現する専用の機能が用意さ...
注意:このメカニズムはたしかに「構造化されたオブジェクト...
追記:Rは、設計に、Lisp(or Scheme)の影響を受けているよ...
追記へのコメント(2006.10.09) 現在 R の言語仕様はこのペー...
終了行:
COLOR(magenta){SIZE(15){オブジェクトの属性とクラス}}
// 間瀬 2004.04.28
R のオブジェクトにはCOLOR(blue){属性(attribute)}と呼ばれ...
> (x <- 1:8) # 等差数列ベクトル
[1] 1 2 3 4 5 6 7 8
> dim(x) <- c(2,4) # x の次元属性を c(2,4) ...
> x # 2x4 行列になる
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> dim(x) <- c(2,2,2) # x の次元属性を c(2,2,2)...
> x # 3次元 (2x2x2) 配列になる
, , 1 # x[,,1] 部分の行列
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2 # x[,,2] 部分の行列
[,1] [,2]
[1,] 5 7
[2,] 6 8
> x[7] # しかし依然としてベクト...
[1] 7
属性は隠された情報であり、COLOR(red){attr()} 関数や COLOR...
> x <- lm(1:5 ~ rnorm(5)) # 例示用の簡単な...
> comment(x) <- "Example (2004.4.19)" # x にコメント属...
> x # 結果の出力(属性...
Call:
lm(formula = 1:5 ~ rnorm(5))
Coefficients:
(Intercept) rnorm(5)
2.7041 -0.5122
> attributes(x) # オブジェクトの...
$names # names(名前)属性...
[1] "coefficients" "residuals" "effects" "rank"
[5] "fitted.values" "assign" "qr" "df.residual"
[9] "xlevels" "call" "terms" "model"
$class # クラス属性
[1] "lm"
$comment
[1] "Example (2004.4.19)" # コメント属性の...
> str(x) # str() 関数はオブジェクトの持つ全情報を簡...
List of 12
$ coefficients : Named num [1:2] 2.704 -0.512
..- attr(*, "names")= chr [1:2] "(Intercept)" "rnorm(5)"
(途中略)
- attr(*, "class")= chr "lm"
R 言語で頻繁に使われる COLOR(red){plot()} 等の関数はCOLOR...
> methods(plot) # 総称的関数 plot() に対す...
[1] plot.HoltWinters* plot.POSIXct plot.POSIXlt
[4] plot.TukeyHSD plot.acf* plot.data.frame
(途中省略)
[28] plot.table plot.ts plot.tskernel*
Non-visible functions are asterisked
> methods(class = "lm") # クラス "lm" のオブジェク...
[1] add1.lm alias.lm anova.lm case.names.lm
[5] coef.lm confint.lm cooks.distance.lm deviance.lm
(途中省略)
[33] variable.names.lm vcov.lm
注意:もし自前のクラスとそのメソッドを定義したければ、概...
(1) クラス名 "hoge" を定義。~
(2) 関数 Hoge() の返り値にクラス属性 "hoge" を与え、さら...
(3) クラス ”hoge" 用の plot メソッド関数 plot.hoge() を定...
(4) R にはこれらを以上の処理を実現する専用の機能が用意さ...
注意:このメカニズムはたしかに「構造化されたオブジェクト...
追記:Rは、設計に、Lisp(or Scheme)の影響を受けているよ...
追記へのコメント(2006.10.09) 現在 R の言語仕様はこのペー...
ページ名: