x <- c(10, 20, 30, 40, 50)
x[1][1] 10
司馬博文
5/07/2021
6/07/2024
A Blog Entry on Bayesian Computation by an Applied Mathematician
$$
$$
R 言語とは
c()が constructorx[]で indexing できる
[]の中身はベクトルで指定できる!これが slice の代わり.matrix() がコンストラクタlist() がコンストラクタdata.frame がコンストラクタ.arrayがコンストラクタtypeof()で調べる
Note that in the C code underlying R, all objects are pointers to a structure with typedef
SEXPREC; the different R data types are represented in C bySEXPTYPE, which determines how the information in the various parts of the structure is used.
int:整数double:クラスとしてはnumericとして実装されている.浮動小数点
3.4e-2は \(3.4×10^-2\) で表す.complex:1iを虚数単位とし,a+biと表す.character
paste(x,y,[sep=“ “])logical:Boole 値print(pi, digit=12)
pi, e:double型Inf, NaN:doule型T,F:TRUE と FALSE の予約.logical 型
as.numeric()で1,0に埋め込まれる.NA:Not Available,統計データの欠損を表す.
NA_integer_, NA_real_, NA_complex_, NA_character_, NA_logical_, ……LETTERS, letters:アルファベットのベクトル,character 型NULL:NULL 型でモードを持たない特殊なオブジェクト.\(\emptyset\) のこと.class()で調べることが出来る.
多くはデータ型(typeof())と一致する.
ベクトルを各成分ごとに評価して,logical vector を返す関数.
!=, >=, <=is.null(), is.na(), is.nan(), is.finite(), is.infinite()identical(1,1.0):Tである,いずれも倍精度浮動小数点で表現されるので.all.equal():「ほぼ同じ」かどうか.数値の近似比較に使える.&,|:論理積・和&&, ||:条件式の論理積・和
&,|と違い,ベクトルには使えない.xor():排他的論理和%in%:match の二項関係版 interface.
"%in%" <- function(x, table) match(x, table, nomatch = 0) > 0が現状の定義with(data, expr, …)
data:data frameexprはdataの列名に関する expression をベタがきexprを実行した結果を返す.match(x, table)
x %in% tableと使える.x:vectortable:vectormode についてstr(obj):R object の構造を教えてくれる.structuremode(obj):オブジェクトのモード=データ型を返す.class(obj):R は全てのものはオブジェクトだから,class を返す.この値に従って dispatch されている.Function
modegives information about the mode of an object in the sense of Becker, Chambers & Wilks (1988), and is more compatible with other implementations of the S language. – R Language Definition
storage.mode(obj):オブジェクトの storage mode を返す.Finally, the function
storage.modereturns the storage mode of its argument in the sense of Becker et al. (1988). – R Language Definition
help():?keywordと等価.
Trig {base}:パッケージ base の Trig についての説明.graphics::hist:はパッケージ graphics の関数 hist について.””で escape する必要があることがある.example():R のこの機能やばすぎる
demo()がさらにある.help.search():??”keyword”と等価.
NULL以外の全てのオブジェクトはattributeを持ち得る.attributeとは,全ての成分に名前がついたpairlistである.
属性は,第一義的には R にクラス構造を実装するのに使われる.class属性を評価し,そのオブジェクトにどの function dispatch を適用するかを決定する.
namesnamesは,ベクトルの各要素に名前をつけるための属性であり,indexing にも使われる.
classclassは,オブジェクトのクラスを指定する文字列である.
R の OO システムは3つあり,S3, S4, R5 という.
S3 implements a style of object oriented programming called generic-function OO. This is different to most programming languages, like Java, C++ and C#, which implement message-passing OO. In message-passing style, messages (methods) are sent to objects and the object determines which function to call. Typically this object has a special appearance in the method call, usually appearing before the name of the method/message: e.g. canvas.drawRect(“blue”). S3 is different. While computations are still carried out via methods, a special type of function called a generic function decides which method to call. Methods are defined in the same way as a normal function, but are called in a different way, as we’ll see shortly.
S3クラス(S言語ver.3という意味) 初期のRクラス構造で現在もRの有力なクラスパラダイム. 組み込みクラスのほとんどがS3パラダイム. 1. 全てがリストであり,新たにクラス名属性を持つとクラスになる. 2. method dispatch機能の実装のためにある 1. 「ジェネリック関数が呼び出された時に,実引数のmodeを見て適切なクラスメソッドに引き渡す機能」があるのでgeneric functionが定義できる. 2. plot()で適切に動くのも,全てのオブジェクトが密かにクラスが違うからである.ジェネリック関数printの呼び出しprint(lm)はlmクラスのメソッドprint.lm()にディスパッチされる.
実装に使われている関数 * attr(): * class():クラス属性のベクトル.ここへ付与する形でS3は実装されている.
それを確認できる関数 * attributes(obj):\(nameや\)classなどの属性を格納したlist. * 実はリストのtag(\(で参照されるやつ)はnamesというattributeである. * それに次いで2番目が\)classというattributeになる. * 通常のlist objectを表示した時,最後の要素がattr(,”class”)となるのは,通常のリストの構造は1列目の要素としたら,2列目の要素ということである. * #これがリストの多次元入れ子構造. * unclass(obj):クラス属性を外せる.lmなど,ほとんどがクラスという属性を持ったlist. * methods(fun):generic methodの全てを表示する. * がついているものは,デフォルトのbase名前空間にはない関数. getAnywhere():あらゆる名前空間からその名前を持つobjectを持ってくる. * 標準名前空間にない存在はnamespace:::nameでアクセスできる. * methods(,”classname”)と使うと,classnameという名前を持ったクラスにdispatchされているmethodを表示する.
class(object) で確認できる.mode(object) <- value は storage mode のこと.
typeof()を wrap している.<- valueとできるのは:“logical”, “integer”, “double”, “complex”, “raw”, “character”, “list”, “expression”, “name”, “symbol” and “function”typeof(object)初期の R クラス構造で現在も R の有力なクラスパラダイム. 組み込みクラスのほとんどが S3 パラダイム. 1. 全てがリストであり,新たにクラス名属性を持つとクラスになる. 2. method dispatch機能の実装のためにある 1. 「ジェネリック関数が呼び出された時に,実引数のmodeを見て適切なクラスメソッドに引き渡す機能」があるのでgeneric functionが定義できる. 2. plot()で適切に動くのも,全てのオブジェクトが密かにクラスが違うからである.ジェネリック関数printの呼び出しprint(lm)はlmクラスのメソッドprint.lm()にディスパッチされる.
実装に使われている関数 * attr(): * class():クラス属性のベクトル.ここへ付与する形でS3は実装されている.
それを確認できる関数 * attributes(obj):\(nameや\)classなどの属性を格納したlist. * 実はリストのtag(\(で参照されるやつ)はnamesというattributeである. * それに次いで2番目が\)classというattributeになる. * 通常のlist objectを表示した時,最後の要素がattr(,”class”)となるのは,通常のリストの構造は1列目の要素としたら,2列目の要素ということである. * #これがリストの多次元入れ子構造. * unclass(obj):クラス属性を外せる.lmなど,ほとんどがクラスという属性を持ったlist. * methods(fun):generic methodの全てを表示する. * がついているものは,デフォルトのbase名前空間にはない関数. getAnywhere():あらゆる名前空間からその名前を持つobjectを持ってくる. * 標準名前空間にない存在はnamespace:::nameでアクセスできる. * methods(,”classname”)と使うと,classnameという名前を持ったクラスにdispatchされているmethodを表示する.
クラス定義のための機構 * print() * prints its argument and returns it invisibly (via ‘invisible(x)’) * 新しいクラスを作ったら,そのためのprintを定義するのが流れ.
S3 クラスとの棲み分けは,安全性で,まだ存在しないクラスコンポーネントにアクセスできなくなった.
c++ならば仮想関数という.↩︎