There are lots of ways to assign a variable etc, and which is best will depend on personal taste. However, a couple of points:
You don't want to be attach()
ing anything. It will work fine 9 times out of 10 and then bite you in the ass when you don't expect it, because all you are doing is placing a copy of your object on the search path. Modify the original object and the one on the search path doesn't change to match.
I personally don't like accessing things with $
in general use. It is ugly and engenders a tendency for users to just delve into objects and rip things out as they wish. Doesn't matter so much for your data, but when I see people doing model$residuals
I get worried. There are better ways (in this case resid()
). Some users also riddle their model formulas with $
.
If you are writing scripts for a data analysis that you might come back to months or years later, anything that can help you understand what your code is doing is an invalable bonus in my opinion. I find with()
and within()
useful for the sort of problem you had because they are explicit about what you want to do.
This is clearer:
x <- data.frame(X = rnorm(10))
with(x, mean(X))
x <- within(x, Y <- rpois(10, 3))
than
x <- data.frame(X = rnorm(10))
mean(x$X)
x$Y <- rpois(10, 3)
## or
x["Y"] <- rpois(10, 3)
Even though they do the same thing.
assign()
inside a within()
call is just a waste of typing, is it not?