📖 Scenario: You work in a small shop and keep track of your products in a table. Sometimes you need to add new information or remove old details from this table.
🎯 Goal: Learn how to add new columns and remove existing columns in an R data frame.
📋 What You'll Learn
Create a data frame with product names and prices
Add a new column for stock quantity
Remove the price column
Print the final data frame
💡 Why This Matters
🌍 Real World
Managing product information in a shop or business often requires updating tables by adding new details or removing outdated ones.
💼 Career
Data analysts and data scientists frequently manipulate data frames to prepare data for analysis or reporting.
Progress0 / 4 steps
1
Create the initial data frame
Create a data frame called products with two columns: name containing "Apple", "Banana", "Carrot" and price containing 1.2, 0.5, 0.3.
R Programming
Hint
Use data.frame() with name = c(...) and price = c(...) to create the table.
2
Add a new column for stock quantity
Add a new column called stock to the products data frame with values 10, 20, 15.
R Programming
Hint
Use products$stock <- c(10, 20, 15) to add the new column.
3
Remove the price column
Remove the price column from the products data frame using the NULL assignment.
R Programming
Hint
Set products$price <- NULL to remove the column.
4
Print the final data frame
Print the products data frame to see the final table with name and stock columns.
R Programming
Hint
Use print(products) to show the table.
Practice
(1/5)
1. In R, how do you add a new column named age with values c(25, 30, 35) to an existing dataframe df?
easy
A. df <- c(df, age = c(25, 30, 35))
B. df[age] = c(25, 30, 35)
C. df$age <- c(25, 30, 35)
D. df$age = NULL
Solution
Step 1: Understand how to add columns in R
In R, you add a new column to a dataframe by assigning a vector to df$column_name.
Step 2: Apply the assignment for the new column
Assigning c(25, 30, 35) to df$age adds the new column age with those values.
Final Answer:
df$age <- c(25, 30, 35) -> Option C
Quick Check:
Use $ and assign vector to add column [OK]
Hint: Use df$column <- values to add columns fast [OK]
Common Mistakes:
Using NULL to add a column instead of removing
Trying to add column with c() outside assignment
Using incorrect dataframe syntax like c(df, ...)
2. Which of the following is the correct syntax to remove the column height from dataframe df in R?
easy
A. df$height <- NULL
B. df$height = c()
C. df$height <- NA
D. remove(df$height)
Solution
Step 1: Recall how to remove columns in R
Assigning NULL to a dataframe column removes it completely.
Step 2: Apply removal syntax
Setting df$height <- NULL deletes the height column from df.
Final Answer:
df$height <- NULL -> Option A
Quick Check:
Assign NULL to remove column [OK]
Hint: Remove columns by assigning NULL: df$col <- NULL [OK]
Common Mistakes:
Assigning empty vector c() instead of NULL
Assigning NA which keeps column but fills with missing
Using remove() which is not for dataframe columns
3. What will be the output of the following R code?
A. Error because data.frame cannot have NULL columns; fix by using list
B. No error; code runs fine and removes y column
C. Error because NULL cannot remove columns; fix by using NA
D. Warning because length of z vector does not match rows; fix by matching length
Solution
Step 1: Check length of new column vector
The dataframe has 3 rows, but c(7, 8) has length 2, causing a length mismatch warning.
Step 2: Fix by providing vector of correct length
Use c(7, 8, 9) or recycle values properly to match 3 rows.
Final Answer:
Warning due to length mismatch; fix by matching vector length -> Option D
Quick Check:
New column vector length must match dataframe rows [OK]
Hint: New column vector length must equal dataframe rows [OK]
Common Mistakes:
Ignoring vector length mismatch causing warning
Thinking NULL cannot remove columns
Trying to assign NA to remove columns
5. You have a dataframe df with columns id, score1, and score2. You want to create a new column average that is the mean of score1 and score2, then remove score1 and score2. Which code correctly does this?