How to add rows to an existing data frame in R
Overview
In this shot, we’ll learn to add a row to an existing data frame.
The data frame in R represents data in a table format with rows and columns.
How to add a row to a data frame
To add a row to an existing data frame, we use the rbind() function.
Syntax
rbind(data frame, row data)
Parameters
The rbind() function takes the following parameter values:
data frame: Represents the data frame object.row data: Represents the row data we wish to add to the data frame.
Return value
The rbind() function returns a modified data frame with a new row.
Example
Let’s illustrate this method with the help of code:
# creating a data frameMy_Data_Frame <- data.frame (Height = c("Tall", "Average", "short"),Body_structure = c("Meso", "Endo", "Ecto"),Age = c(35, 30, 45))# adding a new rowNew_Data_Frame <- rbind(My_Data_Frame, c("Tall", "Meso", 25))New_Data_Frame
Explanation
- Line 2: We create a data frame variable,
My_Data_frame, using thedata.frame()function. The data frame contains three columns. - Line 9: We use the
rbind()function and add a new row to the existing data frame. - Line 11: We print the new data frame
New_Data_Frame.