Examples for 'methods::cbind2'


Combine two Objects by Columns or Rows

Aliases: cbind2 rbind2 cbind2-methods cbind2,ANY,ANY-method cbind2,ANY,missing-method rbind2-methods rbind2,ANY,ANY-method rbind2,ANY,missing-method

Keywords: array manip

### ** Examples

cbind2(1:3, 4)
     [,1] [,2]
[1,]    1    4
[2,]    2    4
[3,]    3    4
m <- matrix(3:8, 2,3, dimnames=list(c("a","b"), LETTERS[1:3]))
cbind2(1:2, m) # keeps dimnames from m
    A B C
a 1 3 5 7
b 2 4 6 8
## rbind() and cbind() now make use of rbind2()/cbind2() methods
setClass("Num", contains="numeric")
setMethod("cbind2", c("Num", "missing"),
          function(x,y, ...) { cat("Num-miss--meth\n"); as.matrix(x)})
setMethod("cbind2", c("Num","ANY"), function(x,y, ...) {
    cat("Num-A.--method\n") ; cbind(getDataPart(x), y, ...) })
setMethod("cbind2", c("ANY","Num"), function(x,y, ...) {
    cat("A.-Num--method\n") ; cbind(x, getDataPart(y), ...) })

a <- new("Num", 1:3)
trace("cbind2")
cbind(a)
trace: cbind2
Num-miss--meth
     a
[1,] 1
[2,] 2
[3,] 3
cbind(a, four=4, 7:9)# calling cbind2() twice
trace: cbind2
trace: cbind2
Num-A.--method
     a four  
[1,] 1    4 7
[2,] 2    4 8
[3,] 3    4 9
cbind(m,a, ch=c("D","E"), a*3)
trace: cbind2
Num-miss--meth
trace: cbind2
trace: cbind2
Num-A.--method
Warning in cbind(getDataPart(x), y, ...): number of rows of result is not a
multiple of vector length (arg 1)
trace: cbind2
  A   B   C   a   ch     
a "3" "5" "7" "1" "D" "3"
b "4" "6" "8" "2" "E" "6"
cbind(1,a, m) # ok with a warning
trace: cbind2
Num-A.--method
Warning in cbind(getDataPart(x), y, ...): number of rows of result is not a
multiple of vector length (arg 1)
trace: cbind2
    a A B C
a 1 1 3 5 7
b 1 2 4 6 8
untrace("cbind2")

[Package methods version 4.2.3 Index]