[R] 1.8.1 and subsetting dataframes

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Nov 24 23:36:32 CET 2003


Patrick Connolly <p.connolly at hortresearch.co.nz> writes:

> I've encountered something that didn't arise using earlier versions of
> R (Linux).
> 
> A dataframe is created and new columns added to it by doing
> calculations using apply with various functions on some of the
> original columns.  It's somewhat too involved to give a toy example
> that's reproducible.  

You can try harder, though. See below.

> However, the resulting phenemonon can be
> characterised by the following:
> 
> Browse[1]> dim(mod.df)
> [1] 409   5
> Browse[1]> object.size(mod.df)
> [1] 31520
> Browse[1]> is.array(mod.df)
> [1] FALSE
> Browse[1]> mod.df[1:5,]
> Error in as.data.frame.default(x[[i]], optional = TRUE) : 
> 	can't coerce array into a data.frame

Looks like one of the columns of mod.df is not what is should have
been. So what does str(mod.df) say?. Also, just before the subsetting,
try setting debug(as.data.frame.default) and see what its argument is
in the case that fails.
 
> The whole dataframe would display correctly, so I figured it couldn't
> have much wrong with it.  

My bet is that it does...

> I suspect it could have something to do with some of the changes
> mentioned in this part of the NEWS file:
> 
>     o	Subscripting for data.frames has been rationalized:
> 
> But I'm not smart enough to see what in those dozen or so would have a
> bearing on this case.  I don't think the drop argument comes into what
> I've done.

Note that this was changed already in 1.8.0, which you say have no
problems... 

My guess is that the code is not quite smart enough yet, e.g.

> x <-data.frame(a=0:9,b=2:11)
> x$b <- array(1:10,10)
> x
Error in as.data.frame.default(x[[i]], optional = TRUE) :
        can't coerce array into a data.frame

but it's not like that has worked before (certainly not in 1.7.1
anyway). One difference is that indexing used to clean up this kind of
corrupted data frame, but now it gives you a data frame which is
corrupted in the same way:

1.8.0 (x as above):

> z <- x[1:5,]
> z
  a b
1 0 1
2 1 2
3 2 3
4 3 4
5 4 5
> x
Error in as.data.frame.default(x[[i]], optional = TRUE) :
        can't coerce array into a data.frame

1.8.1:

> z <- x[1:5,]
> z
Error in as.data.frame.default(x[[i]], optional = TRUE) :
        can't coerce array into a data.frame


> If that's not sufficient to give anyone a hint what could be
> happening, I'll have another attempt to get a toy version.
> 
> 
> Thanks.
> 
> PS: Is there a more elegant way using a text connection instead or
> creating a temporary file in my work around?

Not really. Shouldn't have to do it though. You probably want to put
an as.vector around those apply() calls instead.

-- 
   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




More information about the R-help mailing list