Learn R Programming

dclone (version 1.2-0)

dclone: Cloning R objects

Description

Makes clones of R objects, that is values in the object are repeated $n$ times, leaving the original structure of the object intact (in most of the cases).

Usage

dclone(x, n.clones=1, ...)
## S3 method for class 'default':
dclone(x, n.clones = 1, attrib=TRUE, ...)
## S3 method for class 'dcdim':
dclone(x, n.clones = 1, attrib=TRUE, ...)
## S3 method for class 'list':
dclone(x, n.clones = 1, 
multiply = NULL, unchanged = NULL, attrib=TRUE, ...)
dcdim(x, drop = TRUE, perm = NULL)

Arguments

x
An R object to be cloned, or a cloned object to print.
n.clones
Number of clones.
multiply
Numeric or character index for list element(s) to be multiplied by n.clones instead of repetitions (as done by dclone.default).
unchanged
Numeric or character index for list element(s) to be left unchanged.
attrib
Logical, TRUE if attributes are to be attached.
drop
Logical, if TRUE, deletes the last dimension of an array if that have only one level.
perm
The subscript permutation value, if the cloning dimension is not the last.
...
Other arguments passed to function.

Value

  • An object with class attributes "dclone" plus the original one(s). Dimensions of the original object might change according to n.clones. The function tries to take care of names, sometimes replacing those with the combination of the original names and an integer for number of clones. dcdim sets the class attribute of an object to "dcdim", thus dclone will clone the object by adding an extra dimension for the clones. List (i.e. BUGS data objects) are handled differently to enable element specific determination of the mode of cloning. This can be done via the unchanged and multiply arguments, or by setting the behaviour by the dcdim function.

encoding

UTF-8

Details

dclone is a generic method to clone objects. It is separate from rep, because there are different ways of cloning, depending on the BUGS code implementation: (1) Unchanged: no cloning at all (fo e.g. constants). (2) Repeat: this is the most often used cloning method, repeating the observations as if there were more samples. (3) Multiply: sometimes it is enough to multiply the numbers (e.g. for Binomial distribution). (4) Add dimension: under specific circumstances, it is easier to add another dimension for clones, but othervise repeat the observations (e.g. in case of time series, or for addressing special indexing conventions in the BUGS code, see examples dcdim and dclone.dcdim).

References

Lele, S.R., B. Dennis and F. Lutscher, 2007. Data cloning: easy maximum likelihood estimation for complex ecological models using Bayesian Markov chain Monte Carlo methods. Ecology Letters 10, 551--563.

Examples

Run this code
## scalar
dclone(4, 2)
## vector
(x <- 1:6)
dclone(x, 2)
## matrix
(m <- matrix(x, 2, 3))
dclone(m, 2)
## data frame
(dfr <- as.data.frame(t(m)))
dclone(dfr, 2)
## list
(l <- list(n = 10, y = 1:10, x = 1:10, p = 1))
dclone(l, 2)
dclone(l, 2, attrib = FALSE)
dclone(l, 2, multiply = "n", unchanged = "p")
## effect of dcdim
l$y <- dcdim(l$y)
dclone(l, 2, multiply = "n", unchanged = "p")
## time series like usage of dcdim
z <- data.matrix(rnorm(10))
dclone(dcdim(z), 2)

Run the code above in your browser using DataLab