Exclude Columns

At times we wish to exclude one or more columns and retain the rest. At other times the criteria to describe the columns that we wish to exclude are easier to formulate than their complement (the criteria for columns we wish to retain). In such situations we need to exclude some columns and return the rest. There are four common column exclusion scenarios which we cover in this section:

  1. Exclude by Name: Exclude one or more columns given their names.
  2. Exclude by Position: Exclude one or more columns given their positions.
  3. Select Complement: Exclude columns that do not match a condition.
  4. Exclude Data Type: Exclude columns of one or more data types.

By Name

We wish to exclude one or more columns of a data frame given their names and to return the data frame’s remaining columns.

In this example, we wish to exclude the columns named col_1 and col_5 and return the remaining columns.

df_2 = df_1 %>% select(-col_1, -col_5)

Here is how this works:

  • select() can be used exclude columns by name.
  • To instruct select() to exclude a column, we add a minus - sign before the column name.

By Position

We wish to exclude one or more columns of a data frame given their positions (where 1 is the left most column) and to return the data frame’s remaining columns.

In this example, we wish to exclude the columns at the first and fifth positions and return the remaining columns.

df_2 = df %>% select(-1, -5)

Here is how this works:

  • select() can be used exclude columns by position.
  • To instruct select() to exclude a column, we add a minus - sign before the column position.

Select Complement

We wish to exclude columns that match a condition and return the data frame’s remaining columns.

In this example, we exclude columns that start with old_ and return the remaining columns.

df_2 = df %>% select(!(starts_with('old_')))

Here is how this works:

  • We instruct select() to select columns that match the complement of the condition.
  • We simply add ! prior to the logical expression inside select().

Exclude Data Type

We wish to exclude columns of one or more data types and return the data frame’s remaining columns.

In this example, we wish to exclude both columns of a numeric data type and of a logical data type.

df_2 = df %>% 
    select(
        where(~ !(is.numeric(.) | is.logical(.))))

Here is how this works:

  • We select() the complement of the union (or) of the data types to be excluded.
  • ~ !(is.numeric(.) | is.logical(.))) is an anonymous function ~ returns TRUE for any column whose data type is neither numeric nor logical.
  • where() used inside select() extracts the columns for which the anonymous function returns TRUE.
R
I/O