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:
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.select()
to exclude a column, we add a minus -
sign before the column name.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.select()
to exclude a column, we add a minus -
sign before the column position.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:
select()
to select columns that match the complement of the condition.!
prior to the logical expression inside select()
.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:
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
.