[R] assigning to data frames with whole columns of NAs
Uwe Ligges
ligges at statistik.uni-dortmund.de
Sat Nov 25 12:53:18 CET 2000
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 NA
Right, 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list