I suppose this could be described as a feature (it seems to be similar in S-Plus), but it looks to me more like a bug. Why can't the assignment below to a row of "emptyframe" (or "anotherframe") be made? This with R --vanilla (version info below). Regards -- David David Firth Phone +44 1865 278544 Nuffield College Fax +44 1865 278621 Oxford OX1 1NF Secretary +44 1865 278612 United Kingdom Email david.firth at nuffield.oxford.ac.uk> emptyframe<-data.frame(a=c(NA,NA),b=c(NA,NA)) > emptyframea b 1 NA NA 2 NA NA> emptyframe[1,]<-c(1,2)Warning messages: 1: invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj) 2: invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj)> emptyframea b 1 NA NA 2 NA NA> anotherframe<-data.frame(a=c(8,9),b=c(NA,NA)) > anotherframea b 1 8 NA 2 9 NA> anotherframe[1,]<-c(1,2)Warning message: invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj)> anotherframea b 1 1 NA 2 9 NA> yetanotherframe<-data.frame(a=c(8,NA),b=c(9,NA)) > yetanotherframea b 1 8 9 2 NA NA> yetanotherframe[1,]<-c(1,2) # no problem with this one > yetanotherframea b 1 1 2 2 NA NA> version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 1.1 year 2000 month August day 15 language R -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2000-Nov-25 11:35 UTC
[R] assigning to data frames with whole columns of NAs
David Firth <david.firth at nuffield.oxford.ac.uk> writes:> > emptyframe<-data.frame(a=c(NA,NA),b=c(NA,NA)) > > emptyframe > a b > 1 NA NA > 2 NA NA > > emptyframe[1,]<-c(1,2) > Warning messages: > 1: invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj) > 2: invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj)Problem, what problem?> emptyframea b 1 NA NA 2 NA NA> emptyframe[1,]<-c(1,2) > emptyframea b 1 1 2 2 NA NA OK, I'm pulling your leg. The snag is that NA is logical NA, unless something coerces it to something else, and when constructing data frames, logical vectors are coerced to factors. So I had defined:> emptyframe<-data.frame(a=as.numeric(c(NA,NA)),b=as.numeric(c(NA,NA)))-- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David Firth wrote:> > I suppose this could be described as a feature (it seems to be similar in S-Plus), but it looks to me more like a bug. Why can't the assignment below to a row of "emptyframe" (or "anotherframe") be made? This with R --vanilla (version info below).> > > emptyframe<-data.frame(a=c(NA,NA),b=c(NA,NA)) > > emptyframe > a b > 1 NA NA > 2 NA NA > > emptyframe[1,]<-c(1,2) > Warning messages: > 1: invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj) > 2: invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj)OK. That's not a bug! str(emptyframe) tells us: $a and $b are factors. You cannot assign the *levels* "1" or "2" on this way. So we can generate another similar example: emptyframe <- data.frame(a=c("A", "B"), b=c("A", "B")) str(emptyframe) emptyframe[1,] <- c(1, 2) # Don't work either, what we expect.> > emptyframe > a b > 1 NA NA > 2 NA NA > > anotherframe<-data.frame(a=c(8,9),b=c(NA,NA)) > > anotherframe > a b > 1 8 NA > 2 9 NA > > anotherframe[1,]<-c(1,2) > Warning message: > invalid factor level, NAs generated in: [<-.factor(*tmp*, iseq, value = vjj) > > anotherframe > a b > 1 1 NA > 2 9 NA > > yetanotherframe<-data.frame(a=c(8,NA),b=c(9,NA)) > > yetanotherframe > a b > 1 8 9 > 2 NA NA > > yetanotherframe[1,]<-c(1,2) # no problem with this one > > yetanotherframe > a b > 1 1 2 > 2 NA NARight, because here $a and $b are numeric: str(yetanotherframe) If you really want to generate a data.frame only with NAs in it, you can use for example: emptyframe <- data.frame(a=as.numeric(c(NA, NA)), b=as.numeric(c(NA, NA))) emptyframe[1,] <- c(1, 2) # Works now ... Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._