I have a dataframe called cvar, with two variables (among many others) called MSA and ACTUP. Both are numeric. This was working fine. Then I found out that for two MSAs, ACTUP should be 1, not 0. so I tried cvar$ACTUP[cvar$MSA == 6840] <- 1 cvar$ACTUP[cvar$MSA == 5360] <- 1 but when I try table(cvar$MSA, cvar$ACTUP) the level of ACTUP for those two MSAs has not changed, and is still 0 When I try cvar$MSA or cvar$ACTUP I get lists, as I expect. the MSA list includes 6840 and 5360 when I try cvar$ACTUP[cvar$MSA == 5360] I get numeric(0) Any ideas? What am I missing on a Friday afternoon? Peter Peter L. Flom, PhD Assistant Director, Statistics and Data Analysis Core Center for Drug Use and HIV Research National Development and Research Institutes 71 W. 23rd St www.peterflom.com New York, NY 10010 (212) 845-4485 (voice) (917) 438-0894 (fax)
Prof Brian Ripley
2004-Jan-16 22:01 UTC
[R] Weird problem with trying to change a variable
On Fri, 16 Jan 2004, Peter Flom wrote:> I have a dataframe called cvar, with two variables (among many others) > called MSA and ACTUP. Both are numeric. This was working fine. Then I > found out that for two MSAs, ACTUP should be 1, not 0. > > so I tried > > cvar$ACTUP[cvar$MSA == 6840] <- 1 > cvar$ACTUP[cvar$MSA == 5360] <- 1 > > but when I try > > table(cvar$MSA, cvar$ACTUP) > > the level of ACTUP for those two MSAs has not changed, and is still 0`The level'? You said they were numeric, and it is factors which have levels.> When I try > > cvar$MSA or cvar$ACTUP > > I get lists, as I expect. the MSA list includes 6840 and 5360Lists? Do you mean vectors? Columns of dataframes are not supposed to be lists.> when I try > > cvar$ACTUP[cvar$MSA == 5360] > > I get numeric(0)So presumably cvar$MSA == 5360 is entirely false, but I would check, and I would also check the class of cvar$MSA. If perchance MSA were a factor, something like the following could be happening:> MSA <- factor(c("10000", " 5360")) > MSA[1] 10000 5360 Levels: 5360 10000> MSA == 5360[1] FALSE FALSE -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Thanks for the responses. Several people suggested I check that the numbers are exactly what I think they are (ie. that 5360 is not 5360.00001. I don't think this is the case (the data were entered in SAS, then I used DBMS copy to get them to SPSS, and then read.spss to move them to R), but I will check when I get back to the office (not til Tuesday) In addition, Dr. Ripley replied I wrote> I have a dataframe called cvar, with two variables (among many others) > called MSA and ACTUP. Both are numeric. This was working fine. ThenI> found out that for two MSAs, ACTUP should be 1, not 0. > > so I tried > > cvar$ACTUP[cvar$MSA == 6840] <- 1 > cvar$ACTUP[cvar$MSA == 5360] <- 1 > > but when I try > > table(cvar$MSA, cvar$ACTUP) > > the level of ACTUP for those two MSAs has not changed, and is still 0Brian Ripley replied <<< `The level'? You said they were numeric, and it is factors which have levels.>>>Sorry, I misspoke - I didn't create the data set. ACTUP is, really, a two level variable, but was coded (originally in SAS) as 0 1. when I did mode(ACTUP) I found that it was numeric. Me> when I try > > cvar$ACTUP[cvar$MSA == 5360] > > I get numeric(0)Dr Ripley So presumably cvar$MSA == 5360 is entirely false, but I would check, and I would also check the class of cvar$MSA.>>>when I did mode(MSA) I got that it was numeric Thanks again Peter Peter L. Flom, PhD Assistant Director, Statistics and Data Analysis Core Center for Drug Use and HIV Research National Development and Research Institutes 71 W. 23rd St www.peterflom.com New York, NY 10010 (212) 845-4485 (voice) (917) 438-0894 (fax)