🎓 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 while working with datasets in R, there's a need to add new columns based on the existing data or external information. R provides multiple ways to achieve this. Here, we will demonstrate one of the simplest methods.
2. Program Overview
1. Create an initial dataframe.
2. Add new columns to the dataframe.
3. Code Program
# Load necessary libraries
library(dplyr)
# Create an initial dataframe
df <- data.frame(
Name = c('Alice', 'Bob', 'Charlie'),
Age = c(25, 30, 28)
)
# Print the original dataframe
print("Original Dataframe:")
print(df)
# Add a new column 'Occupation' to the dataframe
df$Occupation <- c('Engineer', 'Doctor', 'Lawyer')
# Add another new column 'Salary' to the dataframe
df$Salary <- c(70000, 80000, 75000)
# Print the updated dataframe
print("Updated Dataframe with New Columns:")
print(df)
Output:
[1] "Original Dataframe:"
Name Age
1 Alice 25
2 Bob 30
3 Charlie 28
[1] "Updated Dataframe with New Columns:"
Name Age Occupation Salary
1 Alice 25 Engineer 70000
2 Bob 30 Doctor 80000
3 Charlie 28 Lawyer 75000
4. Step By Step Explanation
- We start by creating an initial dataframe df with columns Name and Age.
- The new columns are added directly by specifying the column name and assigning the respective values. For instance, df$Occupation is used to add a new column named 'Occupation'.
- Ensure that when adding a new column, the length of the data you're adding matches the number of rows in the dataframe.
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