w3resource

R Programming - Change Multiple Column Names in a Data Frame


Write a R program to change more than one column name of 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'),
  score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
  attempts = c(1, NA, 2, NA, 2, NA, 1, NA, 2, 1),
  qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)

# Print the original data frame
print("Original dataframe:")
print(exam_data)

# Print a message indicating that column names will be changed
print("Change more than one column name of the said dataframe:")

# Change the column name 'name' to 'student_name'
colnames(exam_data)[which(names(exam_data) == "name")] = "student_name"

# Change the column name 'score' to 'avg_score'
colnames(exam_data)[which(names(exam_data) == "score")] = "avg_score"

# Print the data frame after changing column names
print(exam_data)

Output:

[1] "Original dataframe:"
        name score attempts qualify
1  Anastasia  12.5        1     yes
2       Dima   9.0       NA      no
3  Katherine  16.5        2     yes
4      James  12.0       NA      no
5      Emily   9.0        2      no
6    Michael  20.0       NA     yes
7    Matthew  14.5        1     yes
8      Laura  13.5       NA      no
9      Kevin   8.0        2      no
10     Jonas  19.0        1     yes
[1] "Change more than one column name  of the said dataframe:"
   student_name avg_score attempts qualify
1     Anastasia      12.5        1     yes
2          Dima       9.0       NA      no
3     Katherine      16.5        2     yes
4         James      12.0       NA      no
5         Emily       9.0        2      no
6       Michael      20.0       NA     yes
7       Matthew      14.5        1     yes
8         Laura      13.5       NA      no
9         Kevin       8.0        2      no
10        Jonas      19.0        1     yes

Explanation:

  • Create Data Frame:
    • exam_data = data.frame(...):
      • Defines a data frame named exam_data with four columns: name, score, attempts, and qualify.
      • name column contains names of individuals.
      • score column contains their scores.
      • attempts column contains the number of attempts, with some NA values indicating missing data.
      • qualify column indicates whether the individual qualifies or not.
  • Print Original Data Frame:
    • print("Original dataframe:"):
      • Prints a message indicating that the original data frame will be displayed.
    • print(exam_data):
      • Displays the content of the exam_data data frame.
  • Print Column Name Change Message:
    • print("Change more than one column name of the said dataframe:"):
      • Prints a message indicating that multiple column names in the data frame will be changed.
  • Change Column Names:
    • colnames(exam_data)[which(names(exam_data) == "name")] = "student_name":
      • Changes the column name from name to student_name.
    • colnames(exam_data)[which(names(exam_data) == "score")] = "avg_score":
      • Changes the column name from score to avg_score.
  • Print Modified Data Frame:
    • print(exam_data):
      • Displays the exam_data data frame after the column names have been updated.

R Programming Code Editor:



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

Previous: Write a R program to change a column name of a given data frame.
Next: Write a R program to select some random rows from a given data frame.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.