Examples of How To Add and Delete Rows From an R Dataframe

We’re going to walk through how to add and delete rows to a data frame using R.

This article continues the examples started in our data frame tutorial. We’re using the ChickWeight data frame example which is included in the standard R distribution. You can easily get to this by typing: data(ChickWeight) in the R console. This data frame captures the weight of chickens that were fed different diets over a period of 21 days. If you can imagine someone walking around a research farm with a clipboard for an agricultural experiment, you’ve got the right idea….

This series has a couple of parts – feel free to skip ahead to the most relevant parts.

So we’ve shown you how to create and perform basic manipulations on a data frame object using R, including adding and removing data columns and calculated fields. For the next step in our tutorial, we’re going to talk about adding and removing rows.

How to Add a New Row in R

For the first example, we will show you add a row to a dataframe in r. For example, let us suppose we collected one final measurement – day 22 – for our chicken weight data set. We would naturally want to add this into our data frame. Along the same lines, we might also be “healing” a missing data point by adding a record to fill the gap with an appropriate value (real or interpolated). In either event, we would use two R functions to make this work:

  • data.frame() – to create a data frame object holding the rows we want to append
  • rbind() – which is used to append two data frames to each other

Our code for this would look like the following:

# add row to dataframe r
newRow <- data.frame(weight='210',Time='22',Chick='1',Diet='1') 
ChickWeight <- rbind(ChickWeight, newRow)

We hit submit and see the following….

Indicating the process was successful. As you can see, we have inserted a row into the R dataframe immediately following the existing rows. We now have a weight value of 210 inserted for an imaginary 22nd measurement day for the first chick, who was fed diet one.

How to Add Rows To A Dataframe (Multiple)

If we needed to insert multiple rows into a r data frame, we have several options.

First, we can write a loop to append rows to a data frame. This is good if we are doing something like web scraping, where we want to add rows to the data frame after we download each page. We can still use this basic mechanism within a loop, iterating our results and adding new rows to the data frame. And that covers how to add a row to a dataframe in R.

# adding rows - r append to dataframe in loop
# assume scrape_data and parse_page are custom functions

for (url in urlList){
   # scrape the website urls and parse the results
   webpage <- scrape_data (url)
   newRow <-parse_page (webpage)

   # add row to dataframe r
   results <- rbind(results, newRow)
}

You also have the option of using rbind to add multiple rows at once – or even combine two R data frames. If you want to add rows this way, the two data frames need to have the same number of columns.

# add row to dataframe r / adding rows

combined <- rbind(initial, extra)

Add Empty Row to Dataframe

It is generally considered good form to initialize variables before you use them. This may be advisable if you have to perform complex calculations to add a new row to the dataframe. In this case, we will create an empty row that you can populate at a later date.

We accomplish this by taking your existing dataframe and adding a row, setting the values of this row to NA.

# add empty row to dataframe r

> test
  breaks wool tension
1     26    A       L
2     30    A       L
3     54    A       L
4     25    A       L
5     70    A       L
6     52    A       L

> test[nrow(test)+1,] <- NA
> test
  breaks wool tension
1     26    A       L
2     30    A       L
3     54    A       L
4     25    A       L
5     70    A       L
6     52    A       L
7     NA <NA>    <NA>

If you are looking to create an empty data frame, check out this article.

How to Remove Rows in R (Single, Specific Row)

There is a simple option to remove rows from a data frame – we can identify them by number. Continuing our example below, suppose we wished to purge row 578 (day 21 for chick 50) to address a data integrity problem. We could code this as follows:

# how to remove specific rows in r
# remove rows in r by row number
test <- ChickWeight[-c(578),] 

Yielding the following result.

Where, as you can see, we have surgically removed observation 578 from the sample.

How to Remove Rows in R (Multiple Rows)

For larger data removals, it is generally easier to use the methods recommended for selecting a subset. This allows you to set up rules for deleting rows based on specific criteria. For example, see the item below.

# remove rows in r - subset function with multiple conditions
subset(ChickWeight, Diet==4 && Time == 21)

We are able to use the subset command to delete rows that don’t meet specific conditions.

How to Remove Empty Rows in R

A common condition for deleting rows in r is Null or NA values which indicate the row is effectively an empty row. There are actually several ways to accomplish this – we have an entire article here.

For the sake of this article, we’re going to focus on one: omit. The omit function can be used to quickly drop rows with missing data. Here is an example of using the omit function to clean up your dataframe.

# remove rows in r - drop missing values
> test
  breaks wool tension
1     26    A       L
2     30    A       L
3     54    A       L
4     25    A       L
5     70    A       L
6     52    A       L
7     NA <NA>    <NA>

new test <- na.omit(test)

> test
  breaks wool tension
1     26    A       L
2     30    A       L
3     54    A       L
4     25    A       L
5     70    A       L
6     52    A       L

Next up – how to merge two R data frames. Or if you want to skip ahead….