attach
を使用しない別の理由は、読み取り(アクセス)専用のデータフレームの列の値にアクセスできるようにすることです。その列の現在の値の略語ではありません。二つの例:
> head(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
> attach(cars)
> # convert stopping distance to meters
> dist <- 0.3048 * dist
> # convert speed to meters per second
> speed <- 0.44707 * speed
> # compute a meaningless time
> time <- dist/speed
> # check our work
> head(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
変更がdist
とspeed
が割り当てられたにもかかわらずセットcars
データに行われませんでした。
明示的にデータセットに割り当てられている場合は...コンピューティングtime
で参照される
> head(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
> attach(cars)
> # convert stopping distance to meters
> cars$dist <- 0.3048 * dist
> # convert speed to meters per second
> cars$speed <- 0.44707 * speed
> # compute a meaningless time
> cars$time <- dist/speed
> # compute meaningless time being explicit about using values in cars
> cars$time2 <- cars$dist/cars$speed
> # check our work
> head(cars)
speed dist time time2
1 1.78828 0.6096 0.5000000 0.3408862
2 1.78828 3.0480 2.5000000 1.7044311
3 3.12949 1.2192 0.5714286 0.3895842
4 3.12949 6.7056 3.1428571 2.1427133
5 3.57656 4.8768 2.0000000 1.3635449
6 4.02363 3.0480 1.1111111 0.7575249
dist
とspeed
は、元の(未変換)の値です。 cars
を付けたときの値はcars$dist
,cars$speed
であった。
1つの問題は、(あなたの例では) 'job'と呼ばれるメモリ内の他のオブジェクト、つまり' income'があるかもしれないということです。それらを使用したいが、データフレーム 'x'を' attach() 'したい場合は、オブジェクト' x $ job'と 'job'、または' x $ income'と 'income'を混在させるのは簡単です。 –