Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselecting a column by name

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?

like image 932
Alex Avatar asked Mar 21 '12 13:03

Alex


People also ask

How do I deselect a column by name in R?

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[, !

How do I remove certain columns in R?

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.

How do I change a column name in R?

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.


2 Answers

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"))] 
like image 179
csgillespie Avatar answered Oct 16 '22 16:10

csgillespie


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")] } 
like image 22
BenBarnes Avatar answered Oct 16 '22 15:10

BenBarnes