R cbind Function


cbind() function combines vector, matrix or data frame by columns.

cbind(x1, x2, ..., deparse.level = 1)

x1,x2: vector, matrix, data frames
deparse.level: for non matrix, 0 constructs no labels, 1 or 2 constructs labels from the argument names

data1.csv:


data2.csv:


Read in the data from the file:

>x <- read.csv("data1.csv",header=T,sep=",")
>x2 <- read.csv("data2.csv",header=T,sep=",")


>x3 <- cbind(x,x2)
>x3

Subtype Gender Expression Age City
1 A m -0.54 32 New York
2 A f -0.80 21 Houston
3 B f -1.03 34 Seattle
4 C m -0.41 67 Houston


The row number of the two datasets must be equal.


If two vectors do not have the same length, the elements of the short one will be repeated.
> x <- 1:6
> y <- c(1,2)
> cbind(x,y)
     x y
[1,] 1 1
[2,] 2 2
[3,] 3 1
[4,] 4 2
[5,] 5 1
[6,] 6 2

The deparse.level value determines how the column names generated. The default value of deparse.level is 1:

> cbind(x,5)
     x
[1,] 1 5
[2,] 2 5
[3,] 3 5
[4,] 4 5
[5,] 5 5
[6,] 6 5
> cbind(x,5,deparse.level=0)
     [,1] [,2]
[1,]    1    5
[2,]    2    5
[3,]    3    5
[4,]    4    5
[5,]    5    5
[6,]    6    5
> cbind(x,5,deparse.level=2)
     x 5
[1,] 1 5
[2,] 2 5
[3,] 3 5
[4,] 4 5
[5,] 5 5
[6,] 6 5

endmemo.com © 2024  | Terms of Use | Privacy | Home