Open In App

Combine Vectors, Matrix or Data Frames by Rows in R Language – rbind() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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-1

rbind() Function in R

What 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.



Last Updated : 19 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads