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?
You can also drop or deselect columns by prepending the character vector of column names with the - or ! operators. For e.g., dt[, -c("col1", "col2")] or dt[, !
The most easiest way to drop columns is by using subset() function. In the code below, we are telling R to drop variables x and z. The '-' sign indicates dropping variables. Make sure the variable names would NOT be specified in quotes when using subset() function.
Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.
You can do this using vector subsetting. First, create a dummy data set:
R> dd = data.frame(A = 1:3, B = 1:3, C=1:3, D=1:3)
Then use the !
operator to reverse the selection:
R> dd[ ,!(colnames(dd) == "A")] B C D 1 1 1 1 2 2 2 2 3 3 3 3
Alternatively, you could have:
A slightly shorter version (courtesy of @Tomas):
dd[ , names(dd) != "A"]
To cope with multiple columns (courtesy of @Tyler)
dd[ ,!(colnames(dd) %in% c("A", "B"))]
One could use the which()
function to identify the column to be eliminated.
dd <- data.frame(A = 1:5, B = 1:5, C=1:5) dd[, -which(names(dd) == "A")]
or positively
dd[, which(names(dd) != "A")]
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".
if(any(names(dd) == "A")) { dd[, which(names(dd) != "A")] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With