![]() |
In this article, we will discuss what is cbind function and how it works in the R Programming Language. What is Cbind Function In RIn R, the cbind() function is used to combine multiple vectors, matrices, or data frames by columns. The name “cbind” stands for “column bind,” indicating that it binds or concatenates objects along their columns. This function is commonly used in data manipulation and preprocessing tasks to combine data horizontally. In this article, we’ll explore the cbind() function in R with examples to demonstrate its usage. cbind(..., deparse.level = 1)
Combining Vectors using cbind functionLet’s start with a simple example of combining two vectors using the cbind() function:
Output: [1] 1 2 3
[1] 4 5 6
vector1 vector2
[1,] 1 4
[2,] 2 5
[3,] 3 6 In this example, we create two vectors vector1 and vector2. We then use the cbind() function to combine them by columns, resulting in a matrix where each vector becomes a column. Combining Matrices using cbind functionNext, let’s combine two matrices using the cbind() function:
Output: [,1] [,2]
[1,] 1 3
[2,] 2 4
[,1] [,2]
[1,] 5 7
[2,] 6 8
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8 In this example, we create two matrices matrix1 and matrix2. We then use the cbind() function to combine them by columns, resulting in a new matrix where the columns of matrix1 are followed by the columns of matrix2. Combining Data Frames using cbind functionLastly, let’s combine two data frames using the cbind() function:
Output: A B
1 1 X
2 2 Y
3 3 Z
C D
1 a TRUE
2 b FALSE
3 c TRUE
A B C D
1 1 X a TRUE
2 2 Y b FALSE
3 3 Z c TRUE In this example, we create two data frames df1 and df2. We then use the cbind() function to combine them by columns, resulting in a new data frame where the columns of df1 are followed by the columns of df2. ConclusionThe cbind() function in R is a powerful tool for combining objects by columns, including vectors, matrices, and data frames. By using this function, you can efficiently concatenate data horizontally, which is useful for various data manipulation tasks in R. |
Reffered: https://www.geeksforgeeks.org
R Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |