Combine Vectors, Matrix or Data Frames by Rows in R Language - rbind() Function
Last Updated :
19 Dec, 2023
In this article, we will discuss how we Combine Vectors, Matrices, or Data Frames by Rows in R Programming Language using rbind function.
rbind() Function in RWhat is rbind function?
The rbind
function combines or concatenates data frames or matrices by rows. the rbind stands for row binds it shows that it appends rows of one object to another.
Syntax:
rbind(x1, x2, ..., deparse.level = 1)
Parameters:x1, x2: vector, matrix, data frames
deparse.level: This value determines how the column names are generated. The default value of deparse.level is 1.
Combine Vectors using rbind() Function
We will create two vectors and combine them with the help of rbinf function.
R
# R program to illustrate
# rbind function
# Initializing two vectors
x <- 2:7
y <- c(2, 5)
# print the value of x
x
# print the value of y
y
# Calling rbind() function
rbind(x, y)
Output:
[1] 2 3 4 5 6 7
[1] 2 5
[,1] [,2] [,3] [,4] [,5] [,6]
x 2 3 4 5 6 7
y 2 5 2 5 2 5
Combine Matrix using rbind() Function
We will create two matrix and combine them with the help of rbinf function.
R
# create one matrix
a1<-matrix(1:9,3,3)
a1
# Create second matrix
a2<-matrix(10:18,3,3)
a2
# combine the both metrix
rbind(a1,a2)
Output:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[4,] 10 13 16
[5,] 11 14 17
[6,] 12 15 18
Combine data frame using rbind() Function
We will create two data frame and combine them with the help of rbinf function.
R
# Create the first data frame
df1 <- data.frame(
Name = c("Anurag", "Nishant", "Jayesh"),
Age = c(25, 30, 22),
Score = c(90, 85, 92)
)
df1
# Create the second data frame
df2 <- data.frame(
Name = c("Vipul", "Shivang", "Pratham"),
Age = c(28, 35, 27),
Score = c(88, 91, 89)
)
df2
# Combine the data frames by rows
combined_df <- rbind(df1, df2)
# Display the result
print(combined_df)
Output:
Name Age Score
1 Anurag 25 90
2 Nishant 30 85
3 Jayesh 22 92
Name Age Score
1 Vipul 28 88
2 Shivang 35 91
3 Pratham 27 89
Name Age Score
1 Anurag 25 90
2 Nishant 30 85
3 Jayesh 22 92
4 Vipul 28 88
5 Shivang 35 91
6 Pratham 27 89
Combine data frame having missing values
We will create two data frames having missing values and combine them with the help of rbinf function.
R
# Create the first data frame
df1 <- data.frame(
Name = c("Anurag", "Nishant", "Jayesh"),
Age = c(25, NA, 22),
Score = c(90, 85, NA)
)
df1
# Create the second data frame
df2 <- data.frame(
Name = c("Vipul", "Shivang", "Pratham"),
Age = c(NA, 35, 27),
Score = c(88, NA, 89)
)
df2
# Combine the data frames by rows
combined_df <- rbind(df1, df2)
# Display the result
print(combined_df)
Output:
Name Age Score
1 Anurag 25 90
2 Nishant NA 85
3 Jayesh 22 NA
Name Age Score
1 Vipul NA 88
2 Shivang 35 NA
3 Pratham 27 89
Name Age Score
1 Anurag 25 90
2 Nishant NA 85
3 Jayesh 22 NA
4 Vipul NA 88
5 Shivang 35 NA
6 Pratham 27 89
Here are the some of the examples for Combine Vectors, Matrix or Data Frames by Rows in R Programming Language. for combining rows it is necessary that it contain same amount of columns.
Similar Reads
Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns. 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 i
2 min read
Merge Two Data Frames by common Columns in R Programming - merge() Function merge() function in R Language is used to merge two data frames by common columns. Syntax: merge(arg1, arg2, by.x, by.y) Parameters: arg1 and arg2: Data frames to be merged by.x: Common argument of first data frame by.y: Common argument of second data frame Example 1: Python3 1== # R program to merg
1 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: Python3 1== # R program to convert a data frame # into a nu
2 min read
Apply function to each column of matrix in R In this article, we will explore a method to apply the function to each matrix column by using R Programming Language. How to apply the function to each column of the matrix The function 'apply()' is used to apply the function to each column of the matrix. By using these methods provided by R, it is
3 min read
Concatenate List of Two data.tables Using rbindlist() Function in R In this article, we will be looking at the approach to concatenating a list of two data.tables using rbindlist() function in the R programming language. Concatenate List of Two data.tables Using rbindlist() Function In this method of concatenating a list of two data.tables using the rbindlist() func
3 min read