![]() ![]() But, no, that won't remove variables of dataframes that have been attached. (Part of the problem here is that it is perfectly reasonable to think that when you remove all variables with rm(list = ls()), you have removed all of it. When you issue this restart command, R will close and then immediately reopen with your code ready to run. You won't lose anything important, but you will have erased all of the old stuff, not just what you erase with rm(list = ls()). (You only have to do that once.) Now when you get really frustrated because you can't find the stupid error, go to Session/Restart R and run that. If you are using RStudio, go to RStudio/Preferences and check the box that tells it to always restart windows that were open when you quit. You may have to enter detach(data1) several times to clear everything out. If you are as careless a typist as I am, you may have to run your code four or five times until you get it right. You might think that you can easily get out of this problem by using "detach()." But don't be fooled. RStudio will let you do that very easily with Session/Restart R. (Hint: If you find yourself with this problem, it is probably easiest to simply close R and reopen it with a clean slate. And it is so hard to figure out why you can't get the right answer. And the more you work with a set of data, the more likely you are to end up with some variable that is no longer the variable you want. But when we later detach(data), we are detaching the second set, and that allows the original dv to bounce back again and confuse us completely. In other words, it has temporarily hidden that version of dv. ![]() It does not say that dv replaced the earlier dv. ![]() It says "the following objects are "masked" from data". When you attach a data frame you often get what appears to be an error message in the printout. Where did that come from? I don't want that mean!! That example is a bit foreshortened because you would probably do a bunch of other stuff with each problem while you were at it, which just gives you more time to get forgetful and careless.īut now I have other problems to work on, so I put in some more code, read some more data, and ask for the mean of a variable named "dv." But what happens? I get the mean from the first problem. And I keep studying my code and convince myself that, of course, it must be right-but why is the answer wrong?. That's because I may be addressing the previous version of dv, not the one that goes with this particular problem. Then I try to run my second problem again, but this time I get a really strange mean of dv. 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. Statistical Methods for Psychology-Using R, 9th ed. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |