R dplyr: Drop multiple columns

RDplyr

R Problem Overview


I have a dataframe and list of columns in that dataframe that I'd like to drop. Let's use the iris dataset as an example. I'd like to drop Sepal.Length and Sepal.Width and use only the remaining columns. How do I do this using select or select_ from the dplyr package?

Here's what I've tried so far:

drop.cols <- c('Sepal.Length', 'Sepal.Width')
iris %>% select(-drop.cols)

> Error in -drop.cols : invalid argument to unary operator

iris %>% select_(.dots = -drop.cols)

> Error in -drop.cols : invalid argument to unary operator

iris %>% select(!drop.cols)

> Error in !drop.cols : invalid argument type

iris %>% select_(.dots = !drop.cols)

> Error in !drop.cols : invalid argument type

I feel like I'm missing something obvious because these seems like a pretty useful operation that should already exist. On Github, someone posted a similar issue, and Hadley said to use 'negative indexing'. That's what (I think) I've tried, but to no avail. Any suggestions?

R Solutions


Solution 1 - R

Check the help on select_vars. That gives you some extra ideas on how to work with this.

In your case:

iris %>% select(-one_of(drop.cols))

Solution 2 - R

also try

## Notice the lack of quotes
iris %>% select (-c(Sepal.Length, Sepal.Width))

Solution 3 - R

Beyond select(-one_of(drop.cols)) there are a couple other options for dropping columns using select() that do not involve defining all the specific column names (using the dplyr starwars sample data for some more variety in column names):

starwars %>% 
  select(-(name:mass)) %>%        # the range of columns from 'name' to 'mass'
  select(-contains('color')) %>%  # any column name that contains 'color'
  select(-starts_with('bi')) %>%  # any column name that starts with 'bi'
  select(-ends_with('er')) %>%    # any column name that ends with 'er'
  select(-matches('^f.+s$')) %>%  # any column name matching the regex pattern
  select_if(~!is.list(.)) %>%     # not by column name but by data type
  head(2)

# A tibble: 2 x 2
homeworld species
  <chr>     <chr>  
1 Tatooine  Human  
2 Tatooine  Droid 

Solution 4 - R

Be careful with the select() function, because it's used both in the dplyr and MASS packages, so if MASS is loaded, select() may not work properly. To find out what packages are loaded, type sessionInfo() and look for it in the "other attached packages:" section. If it is loaded, type detach( "package:MASS", unload = TRUE ), and your select() function should work again.

Solution 5 - R

We can try

iris %>% 
      select_(.dots= setdiff(names(.),drop.cols))

Solution 6 - R

For anyone arriving here wanting to drop a range of columns.

Minimal reproducible example

Drop a range of columns like so:

iris %>% 
  select(-(Sepal.Width:Petal.Width)) %>% 
  head

#   Sepal.Length Species
# 1          5.1  setosa
# 2          4.9  setosa
# 3          4.7  setosa
# 4          4.6  setosa
# 5          5.0  setosa
# 6          5.4  setosa

Note:

  • The (, ) around the column names is important and must be used

Solution 7 - R

If you have a special character in the column names, either select or select_ may not work as expected. This property of dplyr of using ".". To refer to the data set in the question, the following line can be used to solve this problem:

drop.cols <- c('Sepal.Length', 'Sepal.Width')
  iris %>% .[,setdiff(names(.),drop.cols)]

Solution 8 - R

Another way is to mutate the undesired columns to NULL, this avoids the embedded parentheses :

head(iris,2) %>% mutate_at(drop.cols, ~NULL)
#   Petal.Length Petal.Width Species
# 1          1.4         0.2  setosa
# 2          1.4         0.2  setosa

Solution 9 - R

I also faced the same issue, but the main error was in including library which has another function definition with the same name as "select()". For me it was clashing with the MASS package select function.

After detaching the MASS library, the error stopped.

Solution 10 - R

You can try

iris %>% select(-!!drop.cols)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNavaneethan SanthanamView Question on Stackoverflow
Solution 1 - RphiverView Answer on Stackoverflow
Solution 2 - RMiguel Rayon GonzalezView Answer on Stackoverflow
Solution 3 - RsbhaView Answer on Stackoverflow
Solution 4 - RDurand SinclairView Answer on Stackoverflow
Solution 5 - RakrunView Answer on Stackoverflow
Solution 6 - RstevecView Answer on Stackoverflow
Solution 7 - Rdineshram mattapalliView Answer on Stackoverflow
Solution 8 - RmoodymudskipperView Answer on Stackoverflow
Solution 9 - RDeep Kiran LokhandeView Answer on Stackoverflow
Solution 10 - RLeftyView Answer on Stackoverflow