You are currently viewing Combine Two or Multiple Vectors in R

To combine/append two or more vectors into a single vector in R, you can use the c() function to combine or concatenate multiple objects into a single vector or list. In this article, I will explain how to append/concatenate multiple vectors into a single vector using multiple approaches of R with well-defined examples.

Let’s create the three character vectors and implement the concatenation upon these vectors.


# Create vectors to append
vec1 = c('sai','ram','deepika','sahithi')
vec2 = c('kumar','scott','Don','Lin')
vec3 = c('Ram','Denis') 

R Combine Two Vectors using c()

In R, the c() function takes one or more R objects like vectors, or lists and combines them into a single vector or list. It allows multiple types of objects such as numeric, character, logical, or factors and returns the vector or list containing the combined elements.

Let’s pass the two vectors of the same data type into the c() function and get a single vector.


# combine two vectors
vec4 <- c(vec1,vec2)
print(vec4)

Yields below output.

r combine vectors

Combine Two Vectors of Different Data Types

You can also use the c() function to combine the vectors of different data types. You can pass the objects of different types into the c() function, and R coerces them to a common type according to a hierarchy: character > numeric > complex > logical. For example, if you want to combine a character vector with a numeric vector, the numeric elements will be coerced into character vectors.


# combine two vectors of different types
# Create vectors to append
vec1 = c('sai','ram','deepika','sahithi')
vec2 = c(10, 20, 30, 40)

vec4 <- c(vec1,vec2)
print("After combining the vectors:")
print(vec4)

Yields below output.

r combine vectors

Combine Two Vectors using append()

Similarly, you can use the append() function to append elements to a vector, list, or data frame. This function takes vectors as a parameter and combines them into a single vector.


# combine two vectors
vec4 <- append(vec1,vec2)
print("After combining the vectors:")
print(vec4)

# Output:
# [1] "After combining the vectors:"
# [1] "sai"     "sai"     "deepika" "sahithi" "kumar"   "scott"   "Don"     "Lin"    

Combine Multiple Vectors

To combine multiple vectors into a single vector you can also use the append() function that is used to add elements to a vector. Let’s pass the vectors you want to append into this function and get the single vector of combined elements.


# Combine multiple vectors using append()
# Create vectors to append
vec1 = c('sai','sai','deepika','sahithi')
vec2 = c('kumar','scott','Don','Lin')
vec3 = c(vec1, vec2) 
vec4 = c('Ram','Denis') 
vec5 <- append(vec3, vec4)

print("After combining the vectors:")
print(vec5)

# Output:
# [1] "After combining the vectors:"
# [1] "sai"     "sai"     "deepika" "sahithi" "kumar"   "scott"   "Don"     "Lin"     "Ram"    
# [10] "Denis"

Using the union() Function

You can combine the vectors using one more function of R programming is the union() function is used to combine/append two or more vectors by removing duplicate elements. It returns a single vector containing combined unique elements.


# Combine two vectors using union() function
# Create vectors to append
vec1 = c('sai','sai','deepika','sahithi')
vec2 = c('kumar','scott','Don','Lin'
vec4 <- union(vec1,vec2)
print("After combining the vectors:")
print(vec4)

# Output:
# [1] "After combining the vectors:"
# [1] "sai"     "ram"     "deepika" "sahithi" "kumar"   "scott"   "Don"     "Lin" 

Using the rbind() function to combine Vectors as Columns

The cbind() function is used to combine vectors, matrices, or data frames by columns. Let’s pass two vectors into the cbind() function to combine them element-wise as columns.


# Combine two vectors using cbind() function
# Create vectors to append
vec1 = c('sai','ram','deepika','sahithi')
vec2 = c('kumar','scott','Don','Lin')

vec4 <- cbind(vec1,vec2)
print("After combining the vectors:")
print(vec4)

# Output:
# [1] "After combining the vectors:"
#      vec1      vec2   
# [1,] "sai"     "kumar"
# [2,] "ram"     "scott"
# [3,] "deepika" "Don"  
# [4,] "sahithi" "Lin" 

Using the rbind() function to combine Vectors as Rows

The rbind() function is used to combine vectors, matrices, or data frames by rows. Let’s pass two vectors into the rbind() function to combine them element-wise as rows.


# Combine two vectors using rbind() function
# Create vectors to append
vec1 = c('sai','ram','deepika','sahithi')
vec2 = c('kumar','scott','Don','Lin')

vec4 <- rbind(vec1,vec2)
print("After combining the vectors:")
print(vec4)

# Output:
# [1] "After combining the vectors:"
#      [,1]    [,2]    [,3]      [,4]     
# vec1 "sai"   "ram"   "deepika" "sahithi"
# vec2 "kumar" "scott" "Don"     "Lin"    

Using the paste() function to Combine R Vectors

Finally, you can use the paste() function to combine the vectors by character vector. Let’s pass two vectors into the paste() function to combine them element-wise as character vector.


# Combine two vectors using paste() function
# Create vectors to append
vec1 = c('sai','ram','deepika','sahithi')
vec2 = c('kumar','scott','Don','Lin')

vec4 <- paste(vec1,vec2)
print("After combining the vectors:")
print(vec4)

# Output:
# [1] "After combining the vectors:"
# [1] "sai kumar"   "ram scott"   "deepika Don" "sahithi Lin"   

Conclusion

In this article, I have explained the combining/appending two or more vectors into a single vector using different approaches of R such as the c() function, append(), union(), rbind(), cbind(), and merge() function with well-defined examples.

Related Articles

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium