We add or remove columns in a table to change what information it holds. This helps us organize or update data easily.
Adding and removing columns in R Programming
Introduction
Syntax
R Programming
dataframe$new_column <- values # Add a new column dataframe$column_to_remove <- NULL # Remove a column
Use the $ sign to add or remove columns by name.
Assigning NULL to a column removes it from the dataframe.
Examples
R Programming
df <- data.frame(name = c("Anna", "Bob"), age = c(25, 30)) df$city <- c("NY", "LA")
R Programming
df$age <- NULL
R Programming
df["country"] <- "USA"
Sample Program
This program creates a table with names and scores. Then it adds a grade column and removes the score column. Finally, it prints the updated table.
R Programming
df <- data.frame(name = c("Alice", "John"), score = c(90, 85)) # Add a new column 'grade' df$grade <- c("A", "B") # Remove the 'score' column df$score <- NULL print(df)
Important Notes
When adding a column, make sure the number of values matches the number of rows.
Removing a column by assigning NULL deletes it permanently from the dataframe.
Summary
Use $ to add or remove columns in a dataframe.
Assign values to add a column; assign NULL to remove one.
Adding/removing columns helps keep your data organized and relevant.
Practice
1. In R, how do you add a new column named
age with values c(25, 30, 35) to an existing dataframe df?easy
Solution
Step 1: Understand how to add columns in R
In R, you add a new column to a dataframe by assigning a vector todf$column_name.Step 2: Apply the assignment for the new column
Assigningc(25, 30, 35)todf$ageadds the new columnagewith those values.Final Answer:
df$age <- c(25, 30, 35) -> Option CQuick 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
Solution
Step 1: Recall how to remove columns in R
AssigningNULLto a dataframe column removes it completely.Step 2: Apply removal syntax
Settingdf$height <- NULLdeletes theheightcolumn fromdf.Final Answer:
df$height <- NULL -> Option AQuick 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?
df <- data.frame(name = c("Ann", "Bob"), score = c(90, 85))
df$passed <- c(TRUE, FALSE)
df$score <- NULL
dfmedium
Solution
Step 1: Add new column 'passed'
The code adds a logical columnpassedwith values TRUE and FALSE.Step 2: Remove the 'score' column
AssigningNULLtodf$scoredeletes thescorecolumn.Final Answer:
A dataframe with columns: name, passed -> Option AQuick Check:
Added passed, removed score, left name [OK]
Hint: NULL removes columns; new assignment adds columns [OK]
Common Mistakes:
- Thinking score column remains after NULL assignment
- Expecting error on removing existing column
- Confusing TRUE/FALSE vector with numeric
4. Consider this R code snippet:
What is the problem and how to fix it?
df <- data.frame(x = 1:3, y = 4:6) df$z = c(7, 8) df$y <- NULL df
What is the problem and how to fix it?
medium
Solution
Step 1: Check length of new column vector
The dataframe has 3 rows, butc(7, 8)has length 2, causing a length mismatch warning.Step 2: Fix by providing vector of correct length
Usec(7, 8, 9)or recycle values properly to match 3 rows.Final Answer:
Warning due to length mismatch; fix by matching vector length -> Option DQuick 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?hard
Solution
Step 1: Calculate average of two columns row-wise
UserowMeans()on the subsetdf[c("score1", "score2")]to get the mean for each row.Step 2: Remove the original score columns
AssignNULLtodf$score1anddf$score2to delete them.Final Answer:
df$average <- rowMeans(df[c("score1", "score2")]) df$score1 <- NULL df$score2 <- NULL -> Option BQuick Check:
Use rowMeans() for row-wise average, then remove columns [OK]
Hint: Use rowMeans() for row averages, then assign NULL to remove [OK]
Common Mistakes:
- Using mean() which computes overall mean, not row-wise
- Referencing columns without df$ prefix
- Trying to remove columns by assigning NA
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
