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.
NOTE: With the current release of . the search list can contain at most 20 items. Avoid attaching the same data frame more than once. Always detach the data frame as soon as you have finished using its variables.
NOTE: With the current release of . lists and dataframes can only be attached at position 2 or above. It is not possible to directly assign into an attached list or data frame (thus, to some extent they are static). This is likely to change in the next year or so.