[R] behavior of "by"

Sebastian P. Luque spluque at gmail.com
Wed Oct 29 20:09:39 CET 2008


On Wed, 29 Oct 2008 10:49:03 -0700,
Jeff Laake <Jeff.Laake at noaa.gov> wrote:

> Again thanks for the input.  I've been a recipient of this list for
> quite a few years although I don't post often.  It is an invaluable
> resource and I appreciate the effort of all the contributors.  I
> support a lot of software so I know how much work it can be.

> I've seen the "reproducible code" at the bottom of the messages but
> until I got an off-list explanation I had no idea that it meant an
> example in which the "code AND data can be copied and pasted directly
> into R".  I re-read the posting guide and it does suggest providing an
> example. Adding a definition for "reproducible code" might help.


The posting guide already has that (albeit implicitly):

---<---------------cut here---------------start-------------->---
Examples: Sometimes it helps to provide a small example that someone can
actually run. For example:

  If I have a matrix x as follows:
  > x <- matrix(1:8, nrow=4, ncol=2,
                dimnames=list(c("A","B","C","D"), c("x","y"))
  > x
    x y
  A 1 5
  B 2 6
  C 3 7
  D 4 8
  >

  how can I turn it into a dataframe with 8 rows, and three columns
  named `row', `col', and `value', which have the dimension names as the
  values of `row' and `col', like this:
  > x.df
     row col value
  1    A   x      1
   ...
  (To which the answer might be:
  > x.df <- reshape(data.frame(row=rownames(x), x), direction="long",
                    varying=list(colnames(x)), times=colnames(x),
                    v.names="value", timevar="col", idvar="row")
  )

When providing examples, it is best to give an R command that constructs
the data, as in the matrix() expression above. For more complicated data
structures, dump("x", file=stdout()) will print an expression that will
recreate the object x.
---<---------------cut here---------------end---------------->---

Perhaps this should start with:

Examples: It helps to provide reproducible code, i.e. a small example
that someone can actually run. For example:

to link the R-help banner with the posting guide more explicitly.


-- 
Seb



More information about the R-help mailing list