Is there a way to select all columns of a data frame except a column that has a particular name.
It would be the analog of df[, -1]
, except using the column name instead of the index?
Is there a way to select all columns of a data frame except a column that has a particular name.
It would be the analog of df[, -1]
, except using the column name instead of the index?
For deselecting multiple columns you can use the dplyr package. As an example:
this is another way besides what @csgillespie suggested.
You can do this using vector subsetting. First, create a dummy data set:
Then use the
!
operator to reverse the selection:Alternatively, you could have:
A slightly shorter version (courtesy of @Tomas):
To cope with multiple columns (courtesy of @Tyler)
The
subset
function already allows this type of syntax, from the examples on the help page:One could use the
which()
function to identify the column to be eliminated.or positively
However, if there is no column named "A", you would get a data frame with 0 columns and
nrow(dd)
rows. So it would be good to check for the existence of a column named "A".remove A and C
base
solutiondata.table
solution