[R]base::with() : dataから構築された環境内でRの式を評価する関数。可能ならオリジナルデータのコピーを変更する。

Description

データから構築された環境内でRの式(expression)を評価する。可能ならオリジナルデータ(のコピー)を変更する。

使用法

with(data, expr, …)

within(data, expr, …)

引数

data: 環境構築のために使用されるデータ。デフォルトのwith()メソッドの場合、これは環境(environment)、リスト、データフレーム、あるいは整数である(sys.callの中と同様)。within()メソッドの場合、これはリストあるいはデータフレームのリストである。

expr :  評価する式。

: 将来のメソッドに渡される引数。

詳細

with()はデータから構築されたローカル環境内で式を評価するジェネリック関数である。その環境は呼び出し元の環境を親として持つ。これはモデリング関数を簡略化するのに役に立つ。 (註: dataが既に環境である場合、dataはその存在する親とともに使用される)

expr内部での割当(assignment)は構築された環境の中で行わるのであって、ユーザーのワークスペースの中で行われるわけではない。

within()も同様である。ただしwithin()は式の評価後に環境を調べ、データのコピーに対応する修正を加えこれを返す(これは、データフレームに保存できないオブジェクトが生成された場合、データフレームでは失敗する可能性がある。)within()はtransform()の代替として使用可能である。

返り値

with()関数は評価されたexprを返す。within()関数は修正されたオブジェクトを返す。

註(未訳)

For interactive use this is very effective and nice to read. For programming however, i.e., in one’s functions, more care is needed, and typically one should refrain from using with(), as, e.g., variables in data may accidentally override local variables, see the reference.

Further, when using modeling or graphics functions with an explicit data argument (and typically using formulas), it is typically preferred to use the data argument of that function rather than to use with(data, …).

コメント