Hello, and welcome back! In the previous lesson, you learned how to create and inspect data frames in R. Data frames are essential for organizing and analyzing data. Now that you are familiar with the basics, it's time to take the next step — manipulating data frames. This will allow you to modify and manage your data more effectively, enhancing your ability to perform meaningful data analysis.
In this lesson, you'll learn how to:
Here's a simple example to get you started:
R1# Create a data frame 2df <- data.frame( 3 ID = 1:5, 4 Name = c("John", "Jane", "Alex", "Emily", "David"), 5 Age = c(28, 22, 35, 29, 40) 6) 7 8# Add a new column 9df$Salary <- c(50000, 60000, 70000, 80000, 90000) 10 11# Remove a column 12df$Age <- NULL 13 14# Manipulate data 15df$Salary <- df$Salary * 1.10 16 17# Display the manipulated data frame 18print(df)
Note: In many programming languages, NULL
is used to represent the absence of a value or an empty object. When you assign NULL
to a column of a data frame in R, it removes that column from the data frame. We'll see how this works in the practice!
Being able to manipulate data frames is an essential skill for anyone working with data in R. By mastering these techniques, you can:
Manipulating data frames offers you the flexibility to tailor your data exactly how you need it. This will make your subsequent analyses more efficient and insightful.
Are you excited to get hands-on? Let’s move on to the practice section and start manipulating some data frames!