R Program to Add Rows and Columns to a Data Frame

Introduction

Data frames are fundamental data structures in R, often used for storing and manipulating tabular data. There are situations where you may need to add new rows or columns to an existing data frame. This guide will walk you through writing an R program that adds rows and columns to a data frame.

Problem Statement

Create an R program that:

  • Creates an initial data frame.
  • Adds a new row to the data frame.
  • Adds a new column to the data frame.
  • Displays the updated data frame.

Example:

  • Input:
    • Initial data frame with Name, Age, Gender, and Score columns.
    • New row: ("Rajesh", 24, "Male", 89.5)
    • New column: Grade, calculated based on Score.
  • Output: Updated data frame with the new row and column.

Solution Steps

  1. Create the Initial Data Frame: Use the data.frame() function to create an initial data frame.
  2. Add a New Row: Use the rbind() function to add a new row to the data frame.
  3. Add a New Column: Directly assign a new column to the data frame using the $ operator or by indexing.
  4. Display the Updated Data Frame: Use the print() function to display the data frame after the additions.

R Program

# R Program to Add Rows and Columns to a Data Frame
# Author: Ramesh Fadatare

# Step 1: Create the initial data frame
names <- c("Ramesh", "Suresh", "Mahesh", "Ganesh")
ages <- c(25, 30, 22, 28)
genders <- c("Male", "Male", "Male", "Male")
scores <- c(85.5, 90.0, 88.5, 92.0)

students_data <- data.frame(Name = names, Age = ages, Gender = genders, Score = scores)

# Step 2: Add a new row to the data frame
new_row <- data.frame(Name = "Rajesh", Age = 24, Gender = "Male", Score = 89.5)
students_data <- rbind(students_data, new_row)

# Step 3: Add a new column to the data frame
students_data$Grade <- ifelse(students_data$Score >= 90, "A", ifelse(students_data$Score >= 80, "B", "C"))

# Step 4: Display the updated data frame
print("Updated Students Data Frame:")
print(students_data)

Explanation

Step 1: Create the Initial Data Frame

  • The initial data frame students_data is created using the data.frame() function, with columns for Name, Age, Gender, and Score.

Step 2: Add a New Row to the Data Frame

  • A new row is created as a data frame (new_row) with values "Rajesh", 24, "Male", 89.5.
  • The rbind() function is used to bind this new row to the existing data frame, effectively adding the new row at the end.

Step 3: Add a New Column to the Data Frame

  • A new column Grade is added to the data frame. The ifelse() function is used to assign grades based on the Score column:
    • "A" for scores greater than or equal to 90.
    • "B" for scores between 80 and 89.9.
    • "C" for scores less than 80.

Step 4: Display the Updated Data Frame

  • The print() function is used to display the updated data frame with the new row and column.

Output Example

Example:

[1] "Updated Students Data Frame:"
     Name Age Gender Score Grade
1  Ramesh  25  Male  85.5     B
2  Suresh  30  Male  90.0     A
3  Mahesh  22  Male  88.5     B
4  Ganesh  28  Male  92.0     A
5  Rajesh  24  Male  89.5     B

Conclusion

This R program demonstrates how to add rows and columns to an existing data frame. It covers essential operations such as appending rows using rbind() and adding columns by direct assignment. Understanding how to modify data frames is crucial for data manipulation and analysis in R, making this example valuable for anyone learning R programming.

Leave a Comment

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

Scroll to Top