w3resource

R Programming: Add a new Column to a Data Frame

R Programming: Data frame Exercise-8 with Solution

Write a R program to add a new column in a given data frame.

Sample Solution:

R Programming Code:

# Create a data frame with columns: name, score, attempts, and qualify
exam_data = data.frame(
  name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'), # List of names
  score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19), # List of scores
  attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1), # Number of attempts
  qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes') # Qualification status
)

# Print a message indicating the original dataframe
print("Original dataframe:")

# Display the original dataframe
print(exam_data)

# Print a message indicating the new dataframe with the added column
print("New data frame after adding the 'country' column:")

# Add a new column 'country' to the dataframe with all values set to "USA"
exam_data$country = c("USA","USA","USA","USA","USA","USA","USA","USA","USA","USA")

# Display the updated dataframe
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] "New data frame after adding the 'country' column:"
        name score attempts qualify country
1  Anastasia  12.5        1     yes     USA
2       Dima   9.0        3      no     USA
3  Katherine  16.5        2     yes     USA
4      James  12.0        3      no     USA
5      Emily   9.0        2      no     USA
6    Michael  20.0        3     yes     USA
7    Matthew  14.5        1     yes     USA
8      Laura  13.5        1      no     USA
9      Kevin   8.0        2      no     USA
10     Jonas  19.0        1     yes     USA                         

Explanation:

  • Create a data frame (exam_data):
    • Defines a data frame named exam_data with four columns:
      • name: Contains a list of 10 student names.
      • score: Holds the scores corresponding to each student's performance.
      • attempts: Represents the number of attempts made by each student.
      • qualify: Indicates whether each student has qualified ('yes' or 'no').
  • Print the original data frame:
    • Outputs the message "Original dataframe:" to the console.
    • Prints the content of the exam_data data frame, displaying the initial columns (name, score, attempts, and qualify).
  • Add a new column to the data frame:
    • Outputs the message "New data frame after adding the 'country' column:" to the console.
    • Adds a new column named country to the exam_data data frame, setting the value "USA" for all rows.
  • Print the updated data frame:
    • Prints the modified exam_data data frame to the console, which now includes the new country column alongside the original columns.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to extract 3rd and 5th rows with 1st and 3rd columns from a given data frame.
Next: Write a R program to add new row(s) to an existing data frame.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/dataframe/r-programming-data-frame-exercise-8.php