PSYCH 018 Index > Introduction to R (Part 5) > attach problems

attach: The bad news

The attach( ) function comes with two problems. The first is name conflicts, and the second is modifying a variable from an attached data frame.

Name Conflicts

What would happen if you read in another set of data that also had an age column and tried to attach( ) it? You would have a conflict, and you would get a warning message. This example starts off by detaching both data frames; if you haven’t done an attach( ) yet, you will get an error message; just ignore it and continue.

> detach("student")
> detach("score")
> student <- read.csv("http://evc-cit.info/psych018/r_intro/demographics.csv")
> attach(student)
> age # from the attached student data frame
 [1] 22 18 31 23 19 38 38 18 18 22 20 18 20 26 20 27 18 15 25 28 19 18 23 14 20
[26] 20 19 18 22 20 18 18 20 14 28 18 32 21
> score <- read.csv("http://evc-cit.info/psych018/r_intro/conflict.csv")
> score
  age total
1  20   100
2  22    90
3  24    85
4  21    87
> score$age
[1] 20 22 24 21
> attach(score)

        The following object(s) are masked from student :

         age 

> age
[1] 20 22 24 21
> student$age
 [1] 22 18 31 23 19 38 38 18 18 22 20 18 20 26 20 27 18 15 25 28 19 18 23 14 20
[26] 20 19 18 22 20 18 18 20 14 28 18 32 21

In this conflict, the latest data frame you attached “hides” the column from the previous data frame. But no problem; you can always use the $ notation to get the column you want.

Modifying Attached Variables

Let’s say that, for some unknown reason, you wanted to divide all the total column in the score data frame by two. You might be tempted to do this, given that you have already attached the score data frame:

> total <- total / 2
> total
[1] 50.0 45.0 42.5 43.5

But all is not what it seems. If you decide to use the “long version” to see the total, you will get this:

> score$total
[1] 100  90  85  87

What happened? When you said total <- total / 2, R created a brand new object called total and left the data frame alone. The attach( ) shortcut does not apply to names on the left hand side of a <-

If you want to change the contents of a column in a data frame, use the frame$column notation. In the following example, I am using the long form on both sides of the <- to make my meaning absolutely clear.

> score$total <- score$total / 2
> score$total
[1] 50.0 45.0 42.5 43.5

Return to Reading External Data