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 |
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" |
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 |
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.