Categories: Big Data

Learn R – How to Append Rows to Data Frame

This article represents concepts and code samples on how to append rows to a data frame when working with R programming language. Please feel free to comment/suggest if I missed mentioning one or more important points. Also, sorry for the typos.




Following are the key points described later in this article:

  • How to append one or more rows to an empty data frame
  • How to append one or more rows to non-empty data frame

For illustration purpose, we shall use a student data frame having following information:

  
First.Name Age
1     Calvin  10
2      Chris  25
3        Raj  19

How to Append one or more rows to an Empty Data Frame

Following code represents how to create an empty data frame and append a row.

# Create an empty data frame, teachers, with columns as name, and age
# Note stringsAsFactors = FALSE
teachers <- data.frame( "name" = character(), "age" = integer(), stringsAsFactors=FALSE)
# Alternate way to create is to specify 0 as size of vector
teachers <- data.frame( "name" = character(0), "age" = integer(0), stringsAsFactors=FALSE)
# Append a row
teachers[nrow(teachers) + 1, ] <- c( "ted", 50)
teachers[nrow(teachers) + 1, ] <- c( "james", 55)
# Print teachers
teachers

Following will get printed

   name age
1   ted  50
2 james  55

How to Append One or More Rows

Approach 1:Lets say, you have a student data frame consisting of two columns, namely, First name and Age. The need is to add additional rows. Following code demonstrate the way you could add rows to existing data frame.

# Following code uses rbind to append a data frame to existing data frame
student <- rbind( student, data.frame("First Name"="James", "Age"=55))
# View the student data frame
student

Following is the new data frame:

 First.Name Age
1     Calvin  10
2      Chris  25
3        Raj  19
4      James  55

Approach 2: Following is another approach. It assumed that the student data frame was created using stringsAsFactors=FALSE. Note that this is key for following to work.

# Assign a vector to the new row accessed using index, nrow(student) + 1
student[nrow(student)+1,] <- c("John", 55 )
# Print student
student

Following gets printed

   First.Name Age
1     Calvin  10
2      Chris  25
3        Raj  19
4      James  55
5       John  55
Latest posts by Ajitesh Kumar (see all)
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

View Comments

Recent Posts

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

2 weeks ago

Agentic AI Design Patterns Examples

In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…

2 weeks ago

List of Agentic AI Resources, Papers, Courses

In this blog, I aim to provide a comprehensive list of valuable resources for learning…

2 weeks ago

Understanding FAR, FRR, and EER in Auth Systems

Have you ever wondered how systems determine whether to grant or deny access, and how…

3 weeks ago

Top 10 Gartner Technology Trends for 2025

What revolutionary technologies and industries will define the future of business in 2025? As we…

3 weeks ago

OpenAI GPT Models in 2024: What’s in it for Data Scientists

For data scientists and machine learning researchers, 2024 has been a landmark year in AI…

3 weeks ago