Append Operation on Vectors in R Programming
Vectors in R programming is a basic object consisting sequence of homogeneous elements. It can be integer, logical, double, character, complex or raw. In this article, let us discuss different methods to concatenate/append values to a vector in R programming. Values can be appended/concatenated in a vector using 2 methods:
- c() function
- append() function
Append operation using c() function
c() function is a generic function that combines data into a vector or a list.
Syntax: c(…)
Parameters:
…: represents objects to be concatenated
To know about more optional parameters, use below command in console:
help("c")Example 1:
r
# Create a vectorx <- 1:5n <- 6:10# Append using c() functiony <- c(x, n)# Print resultant vectorprint(y) |
Output:
[1] 1 2 3 4 5 6 7 8 9 10
Example 2:
r
# Create vectorx <- 1:5n <- letters[1:5]# Appendy <- c(x, n)# Print resultant vectorprint(y)# Print type of resultant vectortypeof(y)# Print type of other vectorstypeof(x)typeof(n) |
Output:
[1] "1" "2" "3" "4" "5" "a" "b" "c" "d" "e" [1] "character" [1] "integer" [1] "character"
Append operation using append() function
append() function in R is used for merging vectors or adding more elements to a vector.
Syntax: append(x, values)
Parameters:
x: represents a vector to which values has to be appended to
values: represents the values which has to be appended in the vector
Example 1:
r
# Create a vectorx <- 1:5# Append using append() functionx <- append(x, 6:10)# Print resultant vectorprint(x) |
Output:
[1] 1 2 3 4 5 6 7 8 9 10
Example 2:
r
# Create a vectorx <- 1:5y <- letters[1:5]# Append using append() functionx <- append(x, values = y)# Print resultant vectorprint(x) |
Output:
[1] "1" "2" "3" "4" "5" "a" "b" "c" "d" "e"


