🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Often when working with data frames in R, we may need to rename columns for clarity, and consistency, or to avoid column name conflicts. There are multiple ways to achieve this, but one of the most straightforward methods is using the dplyr package.
2. Program Overview
1. Create a sample dataframe.
2. Use the rename() function from the dplyr package to rename specific columns.
3. Code Program
# Loading necessary library
library(dplyr)
# Create a sample dataframe
df <- data.frame(
FirstName = c('Alice', 'Bob', 'Charlie'),
LastName = c('Anderson', 'Brown', 'Clark'),
Age = c(25, 30, 22)
)
# Print the original dataframe
print("Original Dataframe:")
print(df)
# Rename columns using dplyr's rename() function
df_renamed <- df %>%
rename(
First_Name = FirstName,
Last_Name = LastName
)
# Print the renamed dataframe
print("Renamed Dataframe:")
print(df_renamed)
Output:
[1] "Original Dataframe:" FirstName LastName Age 1 Alice Anderson 25 2 Bob Brown 30 3 Charlie Clark 22 [1] "Renamed Dataframe:" First_Name Last_Name Age 1 Alice Anderson 25 2 Bob Brown 30 3 Charlie Clark 22
4. Step By Step Explanation
- We begin by loading the dplyr package, a part of the tidyverse suite of packages, which provides a variety of functions to manipulate data frames.
- A sample dataframe df is created with columns FirstName, LastName, and Age.
- The rename() function from the dplyr package is used to rename columns. In this function, the new name is provided on the left-hand side of the = sign, and the old name is provided on the right-hand side.
- After renaming, both the original and the renamed data frames are printed to see the changes.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment