So I still have the earlier data set there. The first version of "dv" is still attached in the background, so that is the one that would be called up any time I ask for dv. (This often happens when you run your code, find an error message, correct the error, and rerun.) Now you have used attach two times, and you have the first version hidden under the second, Detaching the second allows the first version to pop into availability. But suppose that I forgot to detach data the first time I ran it, but I did detach it the second. If I type mean(dv), I will get an error message telling me that there is no such variable. Great!! The data frame is still there, but not the individual variables. Now, being a good responsible person, I remember to detached that data frame with "detach(data). And in both cases I get the correct means. And suppose I name that data frame "data" as well, and I probably named my dependent variable "dv." Then suppose that I attached that in the code that I am writing and went on to get its mean. Now assume that I go on and read in the next file that I want to work with. In the bad old days when I was writing R code, I would read some data, attach the data frame, and get the mean of the variable named, for example, "dv." BUT, lots of my problems have a variable named "dv" because it stands for "dependent variable." And lots of data frames get named "data" because that seems like such an obvious choice. I use it to add to my complaint that attach() is confusing and shouldn't be used. I'm not really expecting you to memorize this result. >The following object is masked from data: # So the first guy there wins, right? Not necessarilyĮrror: object 'depvar' not found #Because nothing was attached and depvar if I create depvar = 12, that has priority over the attached version Suppose that you read in data using the following command.ĭepvar The following object is masked _by_. But I need to explain why, because it seems like such a nice simple thing to do. So I use it only rarely in my material, and when I do I feel bad. I have used that command for several years, and found that it really is a bad idea. Many books that you might consult on R will include in their code the attach() command.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |