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.
- Inspecting your data
- Ways to Select a Subset of Data From an R Data Frame
- How To Create an R Data Frame
- How To Sort an R Data Frame
- How to Add and Remove Columns
- Renaming Columns
- How To Add and Remove Rows
- How to Merge Two Data Frames
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. If we needed to insert multiple rows in an automated fashion (for example, if web scraping), we could 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.
How to Remove Rows in R
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 test <- ChickWeight[-c(578),]
Yielding the following result.
Where, as you can see, we have surgically removed observation 578 from the sample.
For larger data removals, it is generally easier to use the methods recommended for selecting a subset.
Next up – how to merge two R data frames. Or if you want to skip ahead….