When you attach a data frame, let's call '
dat', you will have two copies of '
dat', one in usual
R memory that can be listed by ls() and another in a special memory space that can be viewed by search(). In a '
dat' data frame which contains x and y where y = x^2, with attach() function you can plot y against x without calling the data frame name. Now create a new variable z = exp(x) while x is a variable in '
dat', but where is z, in '
dat' or elsewhere? The answer is that z is a stand alone vector outside '
dat' data frame, neither in the '
dat' data frame in usual R memory nor the one that can be searched. You can force z to be a variable in '
dat' by specifying dat$z, but where is z?
attach(dat)
plot(y,x)
z <- exp(x)
dat$z <- exp(x)
plot(z,x)
detach(dat)
I introduce transform() function in the previous tips. Here I will introduce another useful function with(). Instead of attaching a data frame, you can manipulate a variable name in a data frame with with() function. Look at the following modification of the codes above.
with(dat,plot(y,x))
dat <- transform(dat,z=exp(x))
with(dat,plot(z,x))
Notice that with() lets you work with variables in a data frame as transform() does. And now you know where z is and you don't forget to detach anything because you never attach an object.