在 R 语言中按列组合向量、矩阵或数据帧 - cbind()函数
R语言中的cbind()
函数用于按列组合指定的向量、矩阵或数据框。
Syntax: cbind(x1, x2, …, deparse.level = 1)
Parameters:
x1, x2: vector, matrix, data frames
deparse.level: This value determines how the column names generated. The default value of deparse.level is 1.
示例 1:
# R program to illustrate
# cbind function
# Initializing two vectors
x <- 2:7
y <- c(2, 5)
# Calling cbind() function
cbind(x, y)
输出:
x y
[1, ] 2 2
[2, ] 3 5
[3, ] 4 2
[4, ] 5 5
[5, ] 6 2
[6, ] 7 5
示例 2:
# R program to illustrate
# cbind function
# Initializing a vector
x <- 1:5
# Calling cbind() function
cbind(x, 4)
cbind(x, 5, deparse.level = 0)
cbind(x, 6, deparse.level = 2)
cbind(x, 4, deparse.level = 6)
输出:
x
[1, ] 1 4
[2, ] 2 4
[3, ] 3 4
[4, ] 4 4
[5, ] 5 4
[, 1] [, 2]
[1, ] 1 5
[2, ] 2 5
[3, ] 3 5
[4, ] 4 5
[5, ] 5 5
x 6
[1, ] 1 6
[2, ] 2 6
[3, ] 3 6
[4, ] 4 6
[5, ] 5 6
[, 1] [, 2]
[1, ] 1 4
[2, ] 2 4
[3, ] 3 4
[4, ] 4 4
[5, ] 5 4