[R] Weird problem with trying to change a variable
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) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [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 5360 Lists? 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("1", " 5360")) > MSA [1] 1 5360 Levels: 5360 1 > MSA == 5360 [1] FALSE FALSE -- Brian D. Ripley, [EMAIL PROTECTED] 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, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Weird problem with trying to change a variable
> Lists? Do you mean vectors? Columns of dataframes are not supposed to be > lists. Ah. I've been using commands like > x <- list(c(1,2,3),c(2,1),c(6,6,1)) > y <- c("A","B","C") > data.frame(I(x),y) x y 1 1, 2, 3 A 22, 1 B 3 6, 6, 1 C This sort of object behaves pretty much as I'd expect it to (using R 1.8.0 for Windows), though I've only made limited use. The x column has mode list but class AsIs. Is this a legitimate use? (The documentation tells me that as.data.frame is a generic method, with many implementations, including one for AsIs; and that the function I will accept any object. I haven't looked into the implementation.) Damon. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Weird problem with trying to change a variable
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.1. 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. 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 Brian 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) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Weird problem with trying to change a variable
On Sat, 17 Jan 2004, Damon Wischik wrote: [Quoting me in reply to something else without the context not attribution.] > > Lists? Do you mean vectors? Columns of dataframes are not supposed to be > > lists. > > Ah. I've been using commands like > > > x <- list(c(1,2,3),c(2,1),c(6,6,1)) > > y <- c("A","B","C") > > data.frame(I(x),y) > x y > 1 1, 2, 3 A > 22, 1 B > 3 6, 6, 1 C > > This sort of object behaves pretty much as I'd expect it to (using R 1.8.0 > for Windows), though I've only made limited use. The x column has mode > list but class AsIs. Is this a legitimate use? Yes. That is not a `bare' list, but a class with a specific method. > (The documentation tells me that as.data.frame is a generic method, with > many implementations, including one for AsIs; and that the function I will > accept any object. I haven't looked into the implementation.) All you need is for the length to match. It is possible to get bare lists into data frames, but as.data.frame.list inserts each column separately and if you do circumvent that (and I don't mean by I()) some strange things can happen. We don't guarantee not to break what currently works in that area, either. -- Brian D. Ripley, [EMAIL PROTECTED] 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, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Weird problem with trying to change a variable
"Peter Flom" <[EMAIL PROTECTED]> writes: > 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.1. 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) Don't think, check! Calculate min(abs(x-5360)) for instance. -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Weird problem with trying to change a variable SOLVED
Thanks to all who responded. In particular, thanks to Jim Lemon who pointed out that just because mode(MSA) returned ''numeric', doesn't mean MSA isn't a factor. It turned out, indeed, to be a factor. Peter __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html