/
cbind_rbind_rv.R
71 lines (63 loc) · 1.99 KB
/
cbind_rbind_rv.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# ========================================================================
# cbind - column bind for rvs
# ========================================================================
# Note: It's inconvenient that we cannot call the generic (default) cbind
# if class attributes are set.
#
# DEBUG: cbind(1, rvnorm(1), rvnorm(1)) causes an error msg
# In cbind(v, unclass(x[[i]]), deparse.level = deparse.level) :
# number of rows of result is not a multiple of vector length (arg 2)
#' Combine random vectors by columns or rows
#'
#' Combines random vectors by columns (\code{cbind.rv}) or rows
#' (\code{rbind.rv}).
#'
#' See \link{cbind} and \link{rbind} for details.
#'
#' @aliases cbind.rv rbind.rv
#' @param \dots vectors or matrices, can be rv objects
#' @param deparse.level (passed on to cbind)
#' @author Jouni Kerman \email{jouni@@kerman.com}
#' @references Kerman, J. and Gelman, A. (2007). Manipulating and Summarizing
#' Posterior Simulations Using Random Variable Objects. Statistics and
#' Computing 17:3, 235-244.
#'
#' See also \code{vignette("rv")}.
#' @keywords classes
#' @examples
#' x <- rvnorm(10)
#' y <- rvnorm(10)
#' cbind(x, y)
#' rbind(x, y)
#' @export
cbind.rv <- function(..., deparse.level = 1)
{
if (deparse.level != 1)
.NotYetUsed("deparse.level != 1")
x <- list(...)
if (length(x)<1) return(NULL)
v <- NULL
for (i in seq(along=x)) {
v <- cbind(v, unclass(x[[i]]), deparse.level=deparse.level)
}
class(v) <- class(rv())
return(v)
}
## ========================================================================
## rvbind.rv - row bind for rvs
## ========================================================================
##
#' @export
rbind.rv <- function(..., deparse.level = 1)
{
if (deparse.level != 1)
.NotYetUsed("deparse.level != 1")
x <- list(...)
if (length(x)<1) return(NULL)
v <- NULL
for (i in seq(along=x)) {
v <- rbind(v, unclass(x[[i]]), deparse.level=deparse.level)
}
class(v) <- class(rv())
return(v)
}