You are currently viewing R Select All Columns Except Column

To select all columns except one or more than one column from a data frame in R, you can use the df[] notation, subset() function, and select() function from the dplyr package. In this article, I will explain how to select all columns except one or a few columns from R data frame.

Let’s create an R DataFrame, run these examples, and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.


# Create dataframe
df=data.frame(id=c(11,22),
              pages=c(32,45),
              name=c("spark","python"),
              chapters=c(76,86),
              price=c(144,553))

# Display the dataframe
print(df)

# Output
#  id pages   name chapters price
#1 11    32  spark       76   144
#2 22    45 python       86   553

Select All Columns Except One Column in R

Let’s start by using square bracket notation to select all columns except one column, based on its index. The syntax df[, columns] is used to select specific columns in R, and if you want to exclude certain columns, you can use the minus (-) operator.

Here’s an example that selects all columns from a DataFrame except for the second one.


# Select All Columns Except Second column
df2 <- df[,-2]
df2

# Output
#   id   name chapters price
# 1 11  spark       76   144
# 2 22 python       86   553

Select All Columns Except Specified Portion

Alternatively, you can use this notation to select all columns except those within a specific portion. In the following example, it selects all columns except those with indexes between 2 and 4.


# Select except range of columns
df2 <- df[,-2:-4]
df2

# Output
#   id price
# 1 11   144
# 2 22   553

R Select All Columns Except for Vector of Columns

You can use the vector to specify the column indexes you want to ignore the columns. The below syntax creates a new data frame that contains all the columns of the original data frame except for the columns specified in a vector.


# Except Multiple columns
df2 <- df[,-c(2,3)]
df2

# Output
#   id chapters price
# 1 11       76   144
# 2 22       86   553

Except Columns Names From the List

You can also exclude columns from an R data frame by referencing a list of column names. To do this, you can use the names() function to get all the column names from the data frame, and then determine if they are in a given list using the %in% operator.


# Except Columns in List
df2 <- df[,!names(df) %in% c("id", "name", "chapters")]
df2

# Output
#   pages price
# 1    32   144
# 2    45   553

Use R base subset()

Using the R base subset() function, you can select columns by removing specified columns from a data frame. Simply, you can pass the data frame object and the columns you want to delete as arguments of the subset() function. It will return a new data frame without specified columns.


# Using subset
df2 <- subset(df, select = -c(id, name, chapters))
df2

Yields the same output as above.

R Select All Columns Except One using dplyr Package

You can use the dplyr package in R to select all columns, except specific ones. dplyr is a popular R package offering a set of verbs designed to streamline data manipulation tasks, which are frequently used by data analysts. To start using it, you need to install the package with install.packages('dplyr') and then load it into your R session with library(dplyr).

The select() function in dplyr allows you to choose specific columns from a data frame. You can use a negation pattern to specify which columns to exclude from the selection.


# Load the dplyr package
library("dplyr")

# selectall except columns using select()
df2 <- df %>% select(-c(id, name, chapters))

# Output
#   pages price
# 1    32   144
# 2    45   553

Complete Example

The following is a complete example of how to select all columns except one or a few columns from the R data frame (data.frame).


# Create dataframe
df=data.frame(id=c(11,22,33,44,55),
              pages=c(32,45,33,22,56),
              name=c("spark","python","R","java","jsp"),
              chapters=c(76,86,11,15,7),
              price=c(144,553,321,567,890))

# Display the dataframe
print(df)

# select Columns except by Index
df2 <- df[,-2]

# select Columns by except Range of columns
df2 <- df[,-2:-4]

# select all except Multiple columns
df2 <- df[,-c(2,3)]

# except Columns in List
df2 <- df[,!names(df) %in% c("id", "name", "chapters")]

# using subset
df2 <- subset(df, select = -c(id, name, chapters))

# Load the dplyr package
library("dplyr")

# using select()
df2 <- df %>% select(-c(id, name, chapters))

Conclusion

In this article, you have learned different ways to select all columns except one/more than one columns from the R data frame. Examples include ignoring columns by name, indexing, selecting columns based on specific conditions, etc. You have also learned how to use the select() function from the dplyr package for this purpose.

Related Articles