[R] Odp: Idioms for empty dataframes
petr.pikal at precheza.cz
Mon Oct 1 18:13:40 CEST 2007
r-help-bounces at r-project.org napsal dne 01.10.2007 18:01:13:
> I'm fairly new to R, coming from a programming background -- it's quite
> nice to work with dataframes, though, as opposed to explicit iteration.
> One thing I've found, which is surprising is that zero-length dataframes
> seem to cause errors:
> > t <- data.frame(bob=c(100))
> > order(t$bob)
>  1
> > t1 <- t[t$bob < 50]
> > order(t1$bob)
> Error in order(na.last, decreasing, ...) :
> argument 1 is not a vector
> I'd expect c() as a result, not an error.
data frames have dimensions so
t1 <- t[t$bob < 50] # works but is different from
t1 <- t[t$bob < 50,]
If subset operation  results i 1 dim object it looses dimension (so as
names), to prevent this call
t1 <- t[t$bob < 50, ,drop=F]
> So I have two questions --
> Is there something important I'm misunderstanding?
> What idioms do experts use to deal with this? Just calling nrow to
> handle the 0 case? Something cleaner?
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help