R Programming: Add new row(s) to an existing data frame
Write a R program to add new row(s) to an existing data frame.
Sample Solution :
R Programming Code :
# Create a data frame 'exam_data' with columns: name, score, attempts, and qualify
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'), # Vector of names
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19), # Vector of scores
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1), # Vector of attempts
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes') # Vector of qualification status
)
# Print the message "Original dataframe:"
print("Original dataframe:")
# Print the original 'exam_data' data frame
print(exam_data)
# Create a new data frame 'new_exam_data' with additional rows
new_exam_data = data.frame(
name = c('Robert', 'Sophia'), # Vector of new names
score = c(10.5, 9), # Vector of new scores
attempts = c(1, 3), # Vector of new attempts
qualify = c('yes', 'no') # Vector of new qualification status
)
# Add the new rows from 'new_exam_data' to 'exam_data' using rbind()
exam_data = rbind(exam_data, new_exam_data)
# Print the message "After adding new row(s) to an existing data frame:"
print("After adding new row(s) to an existing data frame:")
# Print the updated 'exam_data' data frame with new rows
print(exam_data)
Output:
[1] "Original dataframe:" name score attempts qualify 1 Anastasia 12.5 1 yes 2 Dima 9.0 3 no 3 Katherine 16.5 2 yes 4 James 12.0 3 no 5 Emily 9.0 2 no 6 Michael 20.0 3 yes 7 Matthew 14.5 1 yes 8 Laura 13.5 1 no 9 Kevin 8.0 2 no 10 Jonas 19.0 1 yes [1] "After adding new row(s) to an existing data frame:" name score attempts qualify 1 Anastasia 12.5 1 yes 2 Dima 9.0 3 no 3 Katherine 16.5 2 yes 4 James 12.0 3 no 5 Emily 9.0 2 no 6 Michael 20.0 3 yes 7 Matthew 14.5 1 yes 8 Laura 13.5 1 no 9 Kevin 8.0 2 no 10 Jonas 19.0 1 yes 11 Robert 10.5 1 yes 12 Sophia 9.0 3 no
Explanation:
- Create Initial Data Frame:
A data frame named exam_data is created with four columns (name, score, attempts, qualify) containing initial student data. Each column is populated with corresponding values. - Print Original Data Frame Message:
The message "Original dataframe:" is printed to indicate the start of the data frame output. - Display Original Data Frame:
The original exam_data data frame is printed, showing the initial set of rows with student data. - Create New Data Frame for Additional Rows:
A new data frame named new_exam_data is created with two new rows containing additional student data (name, score, attempts, qualify). - Add New Rows to Existing Data Frame:
The rbind() function is used to append the rows from new_exam_data to the original exam_data data frame. - Print Message After Adding Rows:
The message "After adding new row(s) to an existing data frame:" is printed to indicate the updated data frame output. - Display Updated Data Frame:
The updated exam_data data frame, which now includes the new rows, is printed to show the final set of student data.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to add a new column in a given data frame.
Next: Write a R program to drop column(s) by name from a given data frame.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics