# How to Remove Column in R?

Removing columns from data frames is a common operation in R, particularly in data cleaning and preprocessing phases of data analysis. Depending on the scenario and requirement, there are multiple methods to remove columns in R, each catering to different needs. In this extensive article, we will delve into various methods, explaining each in detail.

### 1. Creating an Example DataFrame

Let’s consider an example data frame named data, which will be used to demonstrate different column removal methods.

data <- data.frame(
ID = c(1, 2, 3, 4),
Name = c("John", "Mike", "Sara", "Anna"),
Age = c(25, 30, 22, 29),
Salary = c(5000, 5500, 5200, 5800)
)
print(data)

Output:

  ID Name Age Salary
1  1 John  25   5000
2  2 Mike  30   5500
3  3 Sara  22   5200
4  4 Anna  29   5800

### 2. Remove Column by Index

Columns can be removed by specifying their index in the data frame.

#### Example:

To remove the second column “Name” using its index:

data <- data[, -2]

Output:

  ID Age Salary
1  1  25   5000
2  2  30   5500
3  3  22   5200
4  4  29   5800

Here, -2 represents the negative index of the “Name” column. The negative sign implies the exclusion of this column from the data frame.

### 3. Remove Columns by Range

A range of columns can be removed if the columns are contiguous.

#### Example:

To remove the second and third columns “Name” and “Age”:

data <- data.frame(
ID = c(1, 2, 3, 4),
Name = c("John", "Mike", "Sara", "Anna"),
Age = c(25, 30, 22, 29),
Salary = c(5000, 5500, 5200, 5800)
)

data <- data[, -c(2:3)]
print(data)

Output:

  ID Salary
1  1   5000
2  2   5500
3  3   5200
4  4   5800

Here, c(2:3) creates a vector representing a range of column indices, and the negative sign implies their removal.

### 4. Remove Multiple Columns

Multiple, non-adjacent columns can also be removed by specifying their indices.

#### Example:

To remove the “Name” and “Salary” columns:

data <- data.frame(
ID = c(1, 2, 3, 4),
Name = c("John", "Mike", "Sara", "Anna"),
Age = c(25, 30, 22, 29),
Salary = c(5000, 5500, 5200, 5800)
)

data <- data[, -c(2, 4)]
print(data)

Output:

  ID Age
1  1  25
2  2  30
3  3  22
4  4  29

### 5. Remove Columns by Name

Columns can be removed directly using their names.

#### Example:

data$Age <- NULL print(data) Output:  ID 1 1 2 2 3 3 4 4 Here, assigning NULL to data$Age effectively removes the “Age” column from the data data frame.

### 6. Remove Columns from List

If you have a list of column names that you want to remove, you can use the select function from the dplyr package.

#### Example:

data <- data.frame(
ID = c(1, 2, 3, 4),
Name = c("John", "Mike", "Sara", "Anna"),
Age = c(25, 30, 22, 29),
Salary = c(5000, 5500, 5200, 5800)
)

library(dplyr)
data <- select(data, -c("Name", "Salary"))
print(data)

Output:

  ID Age
1  1  25
2  2  30
3  3  22
4  4  29

### 7. Using subset( ) Function

The subset() function is another versatile method to remove columns.

#### Example:

data <- data.frame(
ID = c(1, 2, 3, 4),
Name = c("John", "Mike", "Sara", "Anna"),
Age = c(25, 30, 22, 29),
Salary = c(5000, 5500, 5200, 5800)
)

data <- subset(data, select = -c(Name, Salary))
print(data)

Output:

  ID Age
1  1  25
2  2  30
3  3  22
4  4  29

This code removes the “Name” and “Salary” columns by specifying them after the select argument with a negative sign.

### 8. Remove Columns Using contains

Columns with specific strings in their names can be removed using contains in conjunction with the select function in dplyr.

#### Example:

If we have a column named “Employee_Age”, to remove columns containing “Age”:

data <- select(data, -contains("Age"))

### 9. Remove Column That Starts With

#### Example:

data <- data.frame(
ID = c(1, 2, 3, 4),
Name = c("John", "Mike", "Sara", "Anna"),
Age = c(25, 30, 22, 29),
Salary = c(5000, 5500, 5200, 5800)
)

data <- select(data, -starts_with("Sal"))
print(data)

Output:

  ID Name Age
1  1 John  25
2  2 Mike  30
3  3 Sara  22
4  4 Anna  29

### 10. Remove Column That Ends With

Similarly, to remove columns ending with a specific string:

#### Example:

To remove columns that end with “me”:

data <- select(data, -ends_with("me"))

Output:

  ID Age
1  1  25
2  2  30
3  3  22
4  4  29

### 11. Remove Columns If It Exists

Sometimes, to avoid errors due to the non-existence of a column, it is better to check whether a column exists before attempting to remove it.

#### Example:

if ("Name" %in% colnames(data)) data\$Name <- NULL

This code first checks if the “Name” column exists in the data data frame and removes it only if it does exist.

### Conclusion

When removing columns, especially using indices, it is crucial to be wary of the data frame structure to avoid accidentally removing essential columns. Using column names is usually safer, as it is explicit and reduces the likelihood of unintentional removals.

Removing columns in R can be efficiently achieved using various methods depending on the requirements and scenarios. Methods range from using indices, ranges, column names, list of columns, to employing functions from external packages like dplyr for more advanced operations. The choice of method and careful execution are crucial to maintaining data integrity and achieving accurate analytical outcomes. By understanding the underlying principles of each method, users can manipulate data frames effectively in R, paving the way for more robust and insightful data analysis.

Posted in RTagged