Examples for 'base::cbind'


Combine R Objects by Rows or Columns

Aliases: cbind rbind cbind.data.frame rbind.data.frame .__H__.cbind .__H__.rbind

Keywords: array manip

### ** Examples

m <- cbind(1, 1:7) # the '1' (= shorter vector) is recycled
m
     [,1] [,2]
[1,]    1    1
[2,]    1    2
[3,]    1    3
[4,]    1    4
[5,]    1    5
[6,]    1    6
[7,]    1    7
m <- cbind(m, 8:14)[, c(1, 3, 2)] # insert a column
m
     [,1] [,2] [,3]
[1,]    1    8    1
[2,]    1    9    2
[3,]    1   10    3
[4,]    1   11    4
[5,]    1   12    5
[6,]    1   13    6
[7,]    1   14    7
cbind(1:7, diag(3)) # vector is subset -> warning
Warning in cbind(1:7, diag(3)): number of rows of result is not a multiple of
vector length (arg 1)
     [,1] [,2] [,3] [,4]
[1,]    1    1    0    0
[2,]    2    0    1    0
[3,]    3    0    0    1
cbind(0, rbind(1, 1:3))
     [,1] [,2] [,3] [,4]
[1,]    0    1    1    1
[2,]    0    1    2    3
cbind(I = 0, X = rbind(a = 1, b = 1:3))  # use some names
  I      
a 0 1 1 1
b 0 1 2 3
xx <- data.frame(I = rep(0,2))
cbind(xx, X = rbind(a = 1, b = 1:3))   # named differently
  I X.1 X.2 X.3
a 0   1   1   1
b 0   1   2   3
cbind(0, matrix(1, nrow = 0, ncol = 4)) #> Warning (making sense)
Warning in cbind(0, matrix(1, nrow = 0, ncol = 4)): number of rows of result is
not a multiple of vector length (arg 1)
     [,1] [,2] [,3] [,4] [,5]
dim(cbind(0, matrix(1, nrow = 2, ncol = 0))) #-> 2 x 1
[1] 2 1
## deparse.level
dd <- 10
rbind(1:4, c = 2, "a++" = 10, dd, deparse.level = 0) # middle 2 rownames
    [,1] [,2] [,3] [,4]
       1    2    3    4
c      2    2    2    2
a++   10   10   10   10
      10   10   10   10
rbind(1:4, c = 2, "a++" = 10, dd, deparse.level = 1) # 3 rownames (default)
    [,1] [,2] [,3] [,4]
       1    2    3    4
c      2    2    2    2
a++   10   10   10   10
dd    10   10   10   10
rbind(1:4, c = 2, "a++" = 10, dd, deparse.level = 2) # 4 rownames
    [,1] [,2] [,3] [,4]
1:4    1    2    3    4
c      2    2    2    2
a++   10   10   10   10
dd    10   10   10   10
## cheap row names:
b0 <- gl(3,4, labels=letters[1:3])
bf <- setNames(b0, paste0("o", seq_along(b0)))
df  <- data.frame(a = 1, B = b0, f = gl(4,3))
df. <- data.frame(a = 1, B = bf, f = gl(4,3))
new <- data.frame(a = 8, B ="B", f = "1")
(df1  <- rbind(df , new))
   a B f
1  1 a 1
2  1 a 1
3  1 a 1
4  1 a 2
5  1 b 2
6  1 b 2
7  1 b 3
8  1 b 3
9  1 c 3
10 1 c 4
11 1 c 4
12 1 c 4
13 8 B 1
(df.1 <- rbind(df., new))
    a B f
o1  1 a 1
o2  1 a 1
o3  1 a 1
o4  1 a 2
o5  1 b 2
o6  1 b 2
o7  1 b 3
o8  1 b 3
o9  1 c 3
o10 1 c 4
o11 1 c 4
o12 1 c 4
1   8 B 1
stopifnot(identical(df1, rbind(df,  new, make.row.names=FALSE)),
          identical(df1, rbind(df., new, make.row.names=FALSE)))
## Don't show: 
## Testing a semi-official use:
d2 <- rbind.data.frame(as.list(df), as.list(new))
d3 <- rbind.data.frame(as.list(df), as.list(new), make.row.names=FALSE)
stopifnot(identical(.row_names_info(d3), -13L))
## no longer: attr(d2, "row.names")[c(1,13)] == c("13", "131")
row.names(d2) <- attr(d3, "row.names")# = 1:13
stopifnot(identical(d2, d3))
## End(Don't show)

[Package base version 4.2.3 Index]