How to Add and Delete Rows From a Data Frame in R

Add and delete rows from a data frame in R

This tutorial explains how to add and delete rows from a data frame in R.

How to Add Rows to a Data Frame in R

Suppose we have the following data frame:

#create fake dataframe with two columns: "x" and "y"
x <- c(1, 2, 3, 4, 5)
y <- c(1, 4, 9, 16, 25)
data <- data.frame(x, y)
data

#  x y
#1 1 1
#2 2 4
#3 3 9
#4 4 16
#5 5 25

If we would like to add a new row to the bottom of this data frame with values x = 6 and y = 36, then we can use the R function rbind (which stands for “row bind”) to bind a new row to the data frame:

#create new row to add to bottom of existing data frame
new_row <- c(6, 36)

#bind new row to the bottom of existing data frame
new_data <- rbind(data, new_row)
new_data

#  x y
#1 1 1
#2 2 4
#3 3 9
#4 4 16
#5 5 25
#6 6 36

If we would like to add multiple new rows to the end of an existing data frame, we can also do so by using rbind:

#create two data frames
data1 <- data.frame(x = c(1, 2, 3, 4, 5), y = c(1, 4, 9, 16, 25))
data2 <- data.frame(x = c(6, 7, 8), y = c(36, 49, 64))

#row bind the two data frames together into one big data frame
combined_data <- rbind(data1, data2)
combined_data

#  x y
#1 1 1
#2 2 4
#3 3 9
#4 4 16
#5 5 25
#6 6 36
#7 7 49
#8 8 64

It’s important to note that rbind only works when the two data frames have the same column names. In the example above, both data1 and data2 had column names “x” and “y”, so R was able to row bind the two data frames together without any problems.

How to Delete Rows in a Data Frame in R

Suppose we have the following data frame:

#create fake data frame with columns "A", "B", and "C"
data <- data.frame(A = c(1, 2, 3, 5), B = c(12, 15, 4, 11), C = c(9, 7, 2, 15))
data

#  A B C
#1 1 12 9
#2 2 15 7
#3 3 4 2
#4 5 11 15

If we would like to delete rows 1 and 4, we could do so by using the following syntax:

#define new data frame with rows 1 and 4 removed
data_modified <- data[-c(1, 4), ]
data_modified

#  A  B C
#2 2 15 7
#3 3  4 2

Alternatively, we could just create a new data frame that only keeps rows 2 and 3:

#define new data frame with rows 1 and 4 removed
data_modified <- data[c(2, 3), ]
data_modified

#  A B C
#2 2 15 7
#3 3 4 2

Or suppose we only wanted to remove the last row of the data frame. We could use the following syntax to do so, no matter how many rows there are in the data frame:

#define new data frame with just the last row removed
data_modified <- data[-nrow(data), ]
data_modified

#  A  B C
#1 1 12 9
#2 2 15 7
#3 3  4 2

Or suppose we wanted to remove the first three rows of the data frame. We could do so using the following syntax:

#define new data frame with first three rows removed
data_modified <- data[-c(1:3), ]
data_modified

#  A  B  C
#4 5 11 15

Fortunately R is flexible, so there is always a way to remove specific rows from a data frame.

Leave a Reply

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