-1

Yesterday I wrote a syntax for R using the following:

attach(stage)

eleven$stage[stadpt == 6 & stadpn == 0 & stadpm == 0] <-0

I have a data frame with merged data frames and everything worked very well. However today I did exactly the same with an addition:

attach(stage)
eleven$stage[locprim < 9 & stadpt == 6 & stadpn == 0 & stadpm == 0] <-0

But now it doesn't work.. I now get errors that it doesn't recognize the variables anymore

> attach(stage)
Error in attach(stage) : object 'stage' not found
> eleven$stage[locprim < 9 & stadpt == 6 & stadpn == 0 & stadpm == 0] <-0
Error in eleven$stage[locprim < 9 & stadpt == 6 & stadpn == 0 & stadpm ==  : 
  object 'locprim' not found

I know I can solve this by entering eleven$ before every variable (and leave out attach(stage) )but it doesn't make sense (and I have a very long code so I would have to do this suddenly a lot of times..). Yesterday it worked perfectly. Do you have any idea why this is the case?

Thank you a lot in advance!!

Best wishes, Anne

AnneG1990
  • 3
  • 3
  • 1
    can't really provide any solution without reproducible example. so just some generic checks then, e.g. have you read your "stage" into R environment? is the variable 'locprim' spelt correctly in your code? – Adam Quek May 11 '16 at 08:48
  • 1
    Please don't use `attach` – akrun May 11 '16 at 08:49
  • "stage" is a new variable but "locprim" is already in the R environment. I guess I will just add `eleven$` to every variable. Thank you! – AnneG1990 May 11 '16 at 09:06

1 Answers1

0

Don't use attach! Never ever! You will smash all your dataframes as you have several, so the chances of rewriting things are quite high.

What I'd try to do is the following:

eleven$stage[eleven$locprim < 9 && stadpt == 6 && eleven$stadpn == 0 && eleven$stadpm == 0] <-0

So do it like you say you know, by writing eleven$ before every variable. You can see more deeply why: here , here , here and here

Hope it clarifies a little bit! :)

Community
  • 1
  • 1
adrian1121
  • 904
  • 2
  • 9
  • 21