<tt>attach()</tt> and <tt>detach()</tt>



next up previous contents
Next: Working with data Up: Data frames Previous: Making data frames

attach() and detach()

The $ notation, such as accountants$statef, for list components is not always very convenient. A useful facility would be somehow to make the components of a list or data frame temporarily visible as variables under their component name, without the need to quote the list name explicitly each time.

The attach() function, as well as having a directory name as its argument, may also have a data frame. Thus suppose lentils is a data frame with three variables lentils$u, lentils$v, lentils$w. The attach

attach(lentils)

places the data frame in the search list at position 2, and provided there are no variables u, v or w in position 1, u, v and w are available as variables from the data frame in their own right. At this point an assignment such as

u <- v+w

does not replace the component u of the data frame, but rather masks it with another variable u in the working directory at position 1 on the search list. To make a permanent change to the data frame itself, the simplest way is to resort once again to the $ notation:

lentils$u <- v+w

However the new value of component u is not visible until the data frame is detached and attached again.

To detach a data frame, use the function

detach()

More precisely, this statement detaches from the search list the entity currently at position 2. Thus in the present context the variables u, v and w would be no longer visible, except under the list notation as lentils$u and so on.



Erik Moledor
Tue Jan 31 21:02:18 EST 1995