Append Value to Vector in R (4 Examples)

 

In this tutorial you’ll learn how to append a new value to a vector in the R programming language.

The table of content is structured as follows:

Let’s get started:

 

Creating Example Data

In this R tutorial, I’ll use the following example vector in R:

x <- rep("a", 5)               # Create example vector
x
# "a" "a" "a" "a" "a"

As you can see based on the output of the RStudio console, our example vector is a character vector and contains five times the value “a”.

 

Example 1: Append Value to Vector with c() Function

The first example show the most common way for the appendage of new elements to a vector in R: The c() function. The c stands for concatenate and the function is used to combine multiple elements into a single data object.

Have a look at the following R syntax:

x1 <- c(x, "b")                # c() function
x1
"a" "a" "a" "a" "a" "b"

The previous R code concatenated the new value “b” to our example vector and stored the new vector in the data object x1.

 

Example 2: Append Value to Vector with append() Function

An alternative to the c function shown in Example 1 is the append function. The append function can be applied as follows:

x2 <- append(x, "b")           # append() function
x2
"a" "a" "a" "a" "a" "b"

The output is the same as in Example 1.

 

Example 3: Append Value to Vector After Last Position

The next alternative is often used within for-loops or while-loops, since it relies on the index of the new element.

With the following R code, we can append a new element after the last index position of our vector:

x3 <- x                        # Replicate input vector
x3[length(x3) + 1] <- "b"      # Add value at last position
x3
"a" "a" "a" "a" "a" "b"

Again, the output is the same.

 

Example 4: Append Value to Empty Vector

We can also apply the R codes of the previous examples to an empty vector. Consider the following example vector:

x_empty <- character()         # Create empty character vector
x_empty
# character(0)

As you can see, the vector is an empty character vector.

Now, we can add a new value “b” to this empty vector as follows:

x_empty1 <- c(x_empty, "b")    # Add value to empty vector
x_empty1
# "b"

 

Video & Further Resources

Note that we used only character vectors in this R tutorial However, we could also apply these R codes to other data types such as numeric, factor, or more complex character strings.

Do you need more information on the examples of this article? Then you may watch the following video which I have published on my YouTube channel. I’m explaining the topics of this article in the video.

 

The YouTube video will be added soon.

 

Furthermore, I can recommend to have a look at the other articles of my homepage.

 

Summary: In this article, I explained how to combine values into a vector in R programming. Don’t hesitate to let me know in the comments section, if you have any further questions.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top