[R] Problem with expand.grid

Duncan Murdoch murdoch at stats.uwo.ca
Tue Dec 22 17:31:51 CET 2009


On 22/12/2009 11:19 AM, Keith Jewell wrote:
> Just confirming it isn't the bug fixed in 2.11.0dev, and giving an even 
> simpler example:
>   

The docs for expand.grid say it works on lists, but they don't mention 
dataframes.  Although a dataframe is in many ways a list with extra 
structure, apparently something in the subsetting done in expand.grid 
assumes it has a regular list, not a dataframe.

So you might get what you want with expand.grid(as.list(dDF)), though 
this doesn't really make sense:  do you really want 10^7 rows, with lots 
of repetition?  I suspect you want to expand the grid of unique values 
in each column, which you could do using

expand.grid(lapply(dDF, unique))

Duncan Murdoch

> R version 2.11.0 Under development (unstable) (2009-12-20 r50794)
>
> > expand.grid(data.frame(y=1:10, t=1:10))
> Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 2L, 3L, 4L, 5L, 6L,  :
>   replacement has 100 rows, data has 10
>
> "Keith Jewell" <k.jewell at campden.co.uk> wrote in message 
> news:hgqqja$rku$1 at ger.gmane.org...
> > Hi All,
> >
> > This example code
> > ----------------
> > dDF <- structure(list(y = c(4.75587, 4.8451, 5.04139, 4.85733, 5.20412,
> > 5.92428, 5.69897, 4.78958, 4, 4), t = c(0, 48, 144, 192, 240,
> > 312, 360, 0, 48, 144), Batch = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1
> > ), T = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2), pH = c(4.6, 4.6, 4.6,
> > 4.6, 4.6, 4.6, 4.6, 4.6, 4.6, 4.6), S = c(0, 0, 0, 0, 0, 0, 0,
> > 0, 0, 0), N = c(0, 0, 0, 0, 0, 0, 0, 80, 80, 80)), .Names = c("y",
> > "t", "Batch", "T", "pH", "S", "N"), row.names = c(NA, 10L), class = 
> > "data.frame")
> > str(dDF)
> > expand.grid(dDF)
> > ------------------------------------
> > 'hangs' for a while and then gives an error
> >
> > Error in `[[<-.data.frame`(`*tmp*`, i, value = c(4.75587, 4.8451, 5.04139, 
> > :
> >  replacement has 10000000 rows, data has 10
> >
> > In NEWS.R-2.11.0dev I read:
> >    o The new (in 2.9.0) 'stringsAsFactors' argument to expand.grid()
> > was not working: it now does work but has default TRUE for
> > backwards compatibility.
> >
> > but I don't think that's relevant, I have no factors.
> >
> > I'm probably being silly. Can anyone point out where?
> >
> > Best...
> >
> > Keith Jewell
> >
> > --please do not edit the information below--
> >
> > Version:
> > platform = i386-pc-mingw32
> > arch = i386
> > os = mingw32
> > system = i386, mingw32
> > status = Patched
> > major = 2
> > minor = 10.1
> > year = 2009
> > month = 12
> > day = 21
> > svn rev = 50796
> > language = R
> > version.string = R version 2.10.1 Patched (2009-12-21 r50796)
> >
> > Windows Server 2003 x64 (build 3790) Service Pack 2
> >
> > Locale:
> > LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United 
> > Kingdom.1252;LC_MONETARY=English_United 
> > Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252
> >
> > Search Path:
> > .GlobalEnv, package:stats, package:graphics, package:grDevices, 
> > package:utils, package:datasets, package:methods, Autoloads, package:base
> >
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list