Removing Columns in a dataframe based on a list

Dear all,

I have the following problem:
I have a dataframe with ~ 300 columns and I want to remove 150 of these columns based on a list of column names.


           Column A   Column B  Column C  Column D ....

Row 1
Row 2
Row 3

Now I have a list (e.g. excel sheet) with columns which need to be removed:

Column A
Column D

In the end the dataframe should look like this:

           Column B  Column C   ....

Row 1
Row 2
Row 3

Since I have 150 columns to remove it is impossible to do it with e.g. contain function all manually,...

Thank you for any ideas!!!:slight_smile:

Happy to help if you can create a toy example of your dataframe and your specific list :slight_smile: Here is a guide on how to make a reprex: FAQ: How to do a minimal reproducible example ( reprex ) for beginners

1 Like
#set up problem
(hiris <- head(iris))

(some_names_to_remove <- as.list(tail(names(hiris),2)))


1 Like

Thank you Nigrrahamuk.

I have followed your solution.

I get the following error:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'select' for signature '"data.frame"'

I am thinking maybe the issue is that my, in your case, head(iris) is a dataframe and I am trying to remove a list (some_names_to_remove) from a dataframe?

Thank you!!

How is your list structured? Is it a list of character vectors?

Yes, a list of characters (Patient Numbers e.g. MT234324C).

So it looks like this:


I think you might not have dplyr, you error looks like its from base::select rather than dplyr::select, but I can't be sure

Thank you.
I have dplyr installed ..

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

So the below is working with the iris Example, I am trying to create a toy dataset for my real dataset, where it is not working:

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union

 df <
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
#> [[1]]
#> [1] "Sepal.Length"
#> [[2]]
#> [1] "Petal.Length"
 df_new <- select(df,
#>   Sepal.Width Petal.Width Species
#> 1         3.5         0.2  setosa
#> 2         3.0         0.2  setosa
#> 3         3.2         0.2  setosa
#> 4         3.1         0.2  setosa
#> 5         3.6         0.2  setosa
#> 6         3.9         0.4  setosa

Created on 2020-12-04 by the reprex package (v0.3.0)

Ah I got it working with adding "dplyr::select" in front of my select!!:slight_smile: :slight_smile: I think there was a conflict. Thanks for making me think!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.