kpierce8 kpierce8 - 2 months ago 7
R Question

Do you use attach() or call variables by name or slicing?

Many intro R books and guides start off with the practice of attaching a

data.frame
so that you can call the variables by name. I have always found it favorable to call variables with
$
notation or square bracket slicing
[,2]
. That way I can use multiple
data.frame
s without confusing them and/or use iteration to successively call columns of interest. I noticed Google recently posted coding guidelines for R which included the line


1) attach: avoid using it


How do people feel about this practice?

Answer

I never use attach. with and within are your friends.

Example code:

> N <- 3
> df <- data.frame(x1=rnorm(N),x2=runif(N))
> df$y <- with(df,{
   x1+x2
 })
> df
          x1         x2          y
1 -0.8943125 0.24298534 -0.6513271
2 -0.9384312 0.01460008 -0.9238312
3 -0.7159518 0.34618060 -0.3697712
> 
> df <- within(df,{
   x1.sq <- x1^2
   x2.sq <- x2^2
   y <- x1.sq+x2.sq
   x1 <- x2 <- NULL
 })
> df
          y        x2.sq     x1.sq
1 0.8588367 0.0590418774 0.7997948
2 0.8808663 0.0002131623 0.8806532
3 0.6324280 0.1198410071 0.5125870

Edit: hadley mentions transform in the comments. here is some code:

 > transform(df, xtot=x1.sq+x2.sq, y=NULL)
       x2.sq       x1.sq       xtot
1 0.41557079 0.021393571 0.43696436
2 0.57716487 0.266325959 0.84349083
3 0.04935442 0.004226069 0.05358049