Introduction

Hello and welcome! Today, we're learning to add and remove rows from data frames in R. This is an essential skill for data manipulation. By the end of the lesson, you'll be proficient in using the rbind() function to add rows and negative indexing to remove rows.

Recap on Data Frames

As you may recall, a data frame in R is a tabular storage medium for data. It's akin to a table in a library, where each book corresponds to a row, and each detail of a book has a column.

R
1students <- data.frame( 2 Name = c("John", "Anna", "Peter", "Laura"), 3 Age = c(12, 13, 11, 12), 4 Grade = c(7, 8, 7, 7) 5) 6print(students)

Here, we're tracking the details of students with a data frame named students.

Adding Rows to Data Frames

The rbind() function in R helps us add new rows to data frames. It requires that the new row aligns structurally with the existing data frame.

Let's add a new student to our data frame.

R
1new_student <- data.frame( 2 Name = "Amy", 3 Age = 12, 4 Grade = 7 5) 6students <- rbind(students, new_student) 7print(students) # We now see Amy's data included in the `students` data frame.
Removing Rows from Data Frames

To remove rows from a data frame, you can use negative indexing. Let's say, for example, that we want to remove "Peter" from the students data frame.

R
1students <- students[-3, ] 2print(students) # Peter is now removed from our `students` data frame.
Handling Errors: str

Errors can arise if the new row's structure does not align with the data frame's structure. Always confirm the structure and data types of your data frame using the str() function. For example, if we use print(str(students)), we will see the following:

Plain text
1'data.frame': 4 obs. of 3 variables: 2 $ Name : chr "John" "Anna" "Peter" "Laura" 3 $ Age : num 12 13 11 12 4 $ Grade: num 7 8 7 7 5NULL
Handling Errors: Error Example

Imagine you're attempting to add a new student to our students data frame but accidentally leave out one of the required columns. For example, if you forget to include the Grade column for the new student:

R
1incomplete_new_student <- data.frame( 2 Name = "Ben", 3 Age = 14 4 # Note: We forgot to include the Grade column for Ben 5)

Trying to add incomplete_new_student to the students data frame using rbind() will produce an error because the columns in incomplete_new_student do not match those in students. R will display a message similar to:

Plain text
1Error in rbind(deparse.level, ...) : 2 numbers of columns of arguments do not match

This error occurs because all rows in a data frame must have the same columns. Always ensure that the new row includes all the columns present in the original data frame.

Lesson Summary

Well done! You've mastered the skill of adding and removing rows from data frames in R. Now is the time for some hands-on practice to solidify your new skill. Onward!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal