Home » R Tutorials » Combine Two Vectors in R

Combine Two Vectors in R

cbind() function and data.frame() function in the base R package is used to combine two vectors in R. Vectors in R hold the multiple values of the same data type. Functions take vectors as input parameters and combine two vectors to data frame or matrix in R.

cbind() function of base R package takes the sequence of vectors and combines them by columns.

cbin() syntax:

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

Where,
... = vectors or matrices.

data.frame() function of base R package is used to create r data frames. It is used as a data structure as it has many properties of matrices or lists.

data.frame() syntax:

data.frame(..., row.names = NULL, check.rows = FALSE,
           check.names = TRUE, fix.empty.names = TRUE,
           stringsAsFactors = FALSE)

In this tutorial, we will discuss how to combine two vectors in R using cbind() and data.frame() function.

Combine Two Vectors in R to Matrix

Using the cbind() function of the base R package, we can combine two vectors to the matrix. cbind() function takes a sequence of vectors as input parameters.

Let’s practice with an example for combining vectors in R.

Create vectors in R using the c() function. name vector contains multiple values of string data type and age vector contains multiple values of integer type.

name = c("Tom","Kim","Sam","Julie","Emily","Chris")
age <- c(18, 20, 17, 21)

Use the cbind() function for combining two vectors in R into a matrix. name and age vectors are passed as arguments in the cbind() function.

# Concetenate two vectors in R into a matrix
student_info <- cbind(name,age)
# Print R object
student_info
# Prints the class of R object
class(student_info)

In the above R code, cbind() combines two vectors and stores them in the student_info object. Print the R object to see the resultant containing two vectors combined by column.

     name    age 
[1,] "Tom"   "18"
[2,] "Kim"   "20"
[3,] "Sam"   "17"
[4,] "Julie" "21"

[1] "matrix"

The class of the R object is a matrix.

Cool Tip: How to convert a data frame to the matrix in R!

Combining Two Vectors in R into Data Frame

Using the data.frame() function of a base R package, we can combine two vectors into a data frame.

Let’s practice with an example of concatenating two vectors to a data frame in R.

Let’s create vectors for employees like names and salaries. Use data.frame() function to combine two vectors in R.

# Create vectors name and salary and use data.frame() to combine them
name = c("Ish","Ted","Mike","Gui")
salary <- c(4500, 3000, 2700,5500)
# Combine vectors in R
emp_info <- data.frame(name,salary)
# Print the data frame
emp_info
# Print the class of the object
class(emp_info)

In the above R code, use the data.frame() function, it combines two vectors into a data frame and prints the object. The class() function gets the class of R object which is the data frame in our case.

  name salary
1  Ish   4500
2  Ted   3000
3 Mike   2700
4  Gui   5500

[1] "data.frame"

Combining Multiple Vectors to Data Frame in R

Using the cbind.data.frame() which is wrapper of data.frame() function, we can combine multiple vectors into a data frame.

Let’s practice with an example to combine multiple vectors to the data frame using cbind.data.frame()

Create multiple vectors and use cbind.data.frame() wrapper method to split matrix columns into data frame arguments and convert character columns to factors.

# Create multiple vectors
name = c("Ish","Ted","Mike","Gui")
salary <- c(4500, 3000, 2700,5500)
age <- c(30,32,28,35)
# combine multiple vectors 
emp_info <- cbind.data.frame(name,salary,age)

emp_info

class(emp_info)

The output of the above R code is:

  name salary age
1  Ish   4500  30
2  Ted   3000  32
3 Mike   2700  28
4  Gui   5500  35

[1] "data.frame"

Conclusion

I hope the above article on how to combine two vectors in R is helpful to you.

Using the cbind() and data.frame() function in R, we can combine two or multiple vectors in R.

You can find more topics about the R Programming tutorials on the DataVisualizr Home page.

Leave a Comment