Bine
December 3, 2020, 11:04am
1
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.
E.g.
Dataframe:
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!!!
Happy to help if you can create a toy example of your dataframe and your specific list 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))
names(hiris)
(some_names_to_remove <- as.list(tail(names(hiris),2)))
#solution
library(dplyr)
select(hiris,
-all_of(unlist(some_names_to_remove)))
1 Like
Bine
December 3, 2020, 2:30pm
4
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?
Bine
December 3, 2020, 2:55pm
6
Yes, a list of characters (Patient Numbers e.g. MT234324C).
So it looks like this:
MT234324C
MT235354d
MT234324g
....
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
Bine
December 3, 2020, 4:00pm
8
Thank you.
I have dplyr installed ..
If you need more specific help, please provide a proper REPR oducible EX ample (reprex) illustrating your issue.
Bine
December 4, 2020, 12:44pm
10
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:
library("dplyr")
#>
#> 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
#Dataframe
df <-as.data.frame(head(iris))
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
some_names_to_remove<-list("Sepal.Length","Petal.Length")
some_names_to_remove
#> [[1]]
#> [1] "Sepal.Length"
#>
#> [[2]]
#> [1] "Petal.Length"
df_new <- select(df,
-all_of(unlist(some_names_to_remove)))
df_new
#> 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)
Bine
December 4, 2020, 12:57pm
11
Ah I got it working with adding "dplyr::select" in front of my select!! I think there was a conflict. Thanks for making me think!
1 Like
system
Closed
December 25, 2020, 12:57pm
12
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.