Append Vector as New Row to Data Frame in R (Example)

This article explains how to bind a vector to the bottom of a data frame in the R programming language.

Example Data

We can create an example data frame as follows:

df <- data.frame(col1 = letters[1:3],               # New data frame
                 col2 = LETTERS[1:3],
                 stringsAsFactors = FALSE)
df                                                  # Display data
#   col1 col2
# 1    a    A
# 2    b    B
# 3    c    C

We can create an example vector as follows:

rw <- c("new1", "new2")                             # Create vector
rw                                                  # Dsiplay vector
# "new1" "new2"

Append Vector as New Row to Data Frame

We can add our vector as new row to our data frame by applying the rbind function:

df_new <- rbind(df, rw)                             # Bind data and row
df_new                                              # Display updated data
#   col1 col2
# 1    a    A
# 2    b    B
# 3    c    C
# 4 new1 new2

The final output is a data frame consisting of our example data plus our example vector.

Further Examples in a Live Video

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

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.
You need to agree with the terms to proceed

Menu
Top