To remove column from dataframe in R, you can use four different ways:

The following methods show how you can do it with syntax.

Method 1: Use subset() with Negative Index

subset(df, select = -n)

Method 2: Use subset() with Column Name

subset(df, select = -column_name)

Method 3: Use Indexing Operator

df[, !names(df) %in% c('column_name')]

Method 4: Use select() Function

library(dplyr)

df %>% select(-column_name)

The following examples show how to remove column from dataframe in R.

Using subset() with Negative Index

Let’s see how we can use subset() function in R:

# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Remove column from dataframe
df <- subset(df,select = -3)

# Print dataframe 
print(df)

Output:

  Start_date Machine_name Reading
1 2000-05-21     Machine1     110
2 1998-03-28     Machine2      97
3 2001-01-19     Machine3      91
4 2003-04-27     Machine4      89
5 2004-11-26     Machine5      80
6 2008-11-25     Machine6      85

Here the output shows dataframe from which Value column is removed using negative index.

Using subset() with Column Name

Let’s see how we can use subset() function to remove column:

# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Remove column from dataframe
df <- subset(df,select = -Reading)

# Print dataframe 
print(df)

Output:

  Start_date Machine_name Value
1 2000-05-21     Machine1   108
2 1998-03-28     Machine2   120
3 2001-01-19     Machine3   135
4 2003-04-27     Machine4    95
5 2004-11-26     Machine5    98
6 2008-11-25     Machine6   105

Here the output shows dataframe from which Reading column is removed.

Using Indexing Operator

Let’s see how we can use indexing operator to remove column:

# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Remove column from dataframe
new_df <- df[, !names(df) %in% c('Value')]  

# Show new dataframe
print(new_df)

Output:

  Start_date Machine_name Reading
1 2000-05-21     Machine1     110
2 1998-03-28     Machine2      97
3 2001-01-19     Machine3      91
4 2003-04-27     Machine4      89
5 2004-11-26     Machine5      80
6 2008-11-25     Machine6      85

Here in the output it shows new dataframe which created from dataframe df but we removed Value column.

Using select() Function

Let’s see how to use select() function in R:

library(dplyr)

# Create dataframe
df <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
                 Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
                 Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Remove column from dataframe
new_df <- df %>% select(-Reading)

# Print dataframe 
print(df)

Output:

  Start_date Machine_name Value
1 2000-05-21     Machine1   108
2 1998-03-28     Machine2   120
3 2001-01-19     Machine3   135
4 2003-04-27     Machine4    95
5 2004-11-26     Machine5    98
6 2008-11-25     Machine6   105

Here in the output it shows new dataframe which created from dataframe df but we removed Reading column.