[R] Problem with expand.grid

Martin Maechler maechler at stat.math.ethz.ch
Wed Dec 23 14:39:47 CET 2009


>>>>> "KJ" == Keith Jewell <k.jewell at campden.co.uk>
>>>>>     on Tue, 22 Dec 2009 16:02:49 -0000 writes:

    KJ> Hi All,
    KJ> This example code
    KJ> ----------------
    KJ> dDF <- structure(list(y = c(4.75587, 4.8451, 5.04139, 4.85733, 5.20412,
    KJ> 5.92428, 5.69897, 4.78958, 4, 4), t = c(0, 48, 144, 192, 240,
    KJ> 312, 360, 0, 48, 144), Batch = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1
    KJ> ), T = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2), pH = c(4.6, 4.6, 4.6,
    KJ> 4.6, 4.6, 4.6, 4.6, 4.6, 4.6, 4.6), S = c(0, 0, 0, 0, 0, 0, 0,
    KJ> 0, 0, 0), N = c(0, 0, 0, 0, 0, 0, 0, 80, 80, 80)), .Names = c("y",
    KJ> "t", "Batch", "T", "pH", "S", "N"), row.names = c(NA, 10L), class = 
    KJ> "data.frame")
    KJ> str(dDF)
    KJ> expand.grid(dDF)
    KJ> ------------------------------------
    KJ> 'hangs' for a while and then gives an error

    KJ> Error in `[[<-.data.frame`(`*tmp*`, i, value = c(4.75587, 4.8451, 5.04139, 
    KJ> :
    KJ> replacement has 10000000 rows, data has 10

    KJ> In NEWS.R-2.11.0dev I read:
    KJ> o	The new (in 2.9.0) 'stringsAsFactors' argument to expand.grid()
    KJ> was not working: it now does work but has default TRUE for
    KJ> backwards compatibility.

    KJ> but I don't think that's relevant, I have no factors.

    KJ> I'm probably being silly.

yes, probably ;-)

At least you are using  expand.grid() in an unusual way.
[What are you trying to use expand.grid() for ?]
E.g., (dataframe) variable 'S' has only values '0'.

One thing which works and may be close to what you want is the
following:

dU <- lapply(dDF, unique)
r <- expand.grid(dU)


Here's the illustration about it :

> str(dDF)
'data.frame':	10 obs. of  7 variables:
 $ y    : num  4.76 4.85 5.04 4.86 5.2 ...
 $ t    : num  0 48 144 192 240 312 360 0 48 144
 $ Batch: num  1 1 1 1 1 1 1 1 1 1
 $ T    : num  2 2 2 2 2 2 2 2 2 2
 $ pH   : num  4.6 4.6 4.6 4.6 4.6 4.6 4.6 4.6 4.6 4.6
 $ S    : num  0 0 0 0 0 0 0 0 0 0
 $ N    : num  0 0 0 0 0 0 0 80 80 80
> 
> str(dU <- lapply(dDF, unique))
List of 7
 $ y    : num [1:9] 4.76 4.85 5.04 4.86 5.2 ...
 $ t    : num [1:7] 0 48 144 192 240 312 360
 $ Batch: num 1
 $ T    : num 2
 $ pH   : num 4.6
 $ S    : num 0
 $ N    : num [1:2] 0 80
> r <- expand.grid(dU)
> str(r)
'data.frame':	126 obs. of  7 variables:
 $ y    : num  4.76 4.85 5.04 4.86 5.2 ...
 $ t    : num  0 0 0 0 0 0 0 0 0 48 ...
 $ Batch: num  1 1 1 1 1 1 1 1 1 1 ...
 $ T    : num  2 2 2 2 2 2 2 2 2 2 ...
 $ pH   : num  4.6 4.6 4.6 4.6 4.6 4.6 4.6 4.6 4.6 4.6 ...
 $ S    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ N    : num  0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "out.attrs")=List of 2
  ..$ dim     : Named int  9 7 1 1 1 1 2
  .. ..- attr(*, "names")= chr  "y" "t" "Batch" "T" ...
  ..$ dimnames:List of 7
  .. ..$ y    : chr  "y=4.75587" "y=4.84510" "y=5.04139" "y=4.85733" ...
  .. ..$ t    : chr  "t=  0" "t= 48" "t=144" "t=192" ...
  .. ..$ Batch: chr "Batch=1"
  .. ..$ T    : chr "T=2"
  .. ..$ pH   : chr "pH=4.6"
  .. ..$ S    : chr "S=0"
  .. ..$ N    : chr  "N= 0" "N=80"
> 

---

may be that helps?

Regards,
Martin Maechler, ETH Zurich




More information about the R-help mailing list