Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a dataframe with columns temporarily removed by name?

Tags:

For example, with the data set mtcars

mtcars[ , "cyl"] 

and

mtcars[ , 2] 

both give me the same column. So, since I can get everything BUT the column 2 like this:

mtcars[ , -2] 

I don't expect this:

mtcars[ , -"cyl"] Error in -"cyl" : invalid argument to unary operator 

instead the best I can come up with is this:

mtcars[ , !colnames(mtcars)=="cyl"] 

Is there an easier solution?

EDIT: It seems logical that if the first two techniques work, so should the second two techniques. I hoped I was missing something. The help pages for ?"[" or ?subset don't explain this counterintuitive result. Does anybody know why this is?

like image 884
J. Win. Avatar asked Jan 29 '11 05:01

J. Win.


People also ask

How do I remove a column from a DataFrame by name in R?

Method 1: Using subset() This is one of the easiest approaches to drop columns is by using the subset() function with the '-' sign which indicates dropping variables. This function in R Language is used to create subsets of a Data frame and can also be used to drop columns from a data frame.

How do you delete specific columns in Python?

The most common way to remove a column is using df. drop() . Sometimes, del command in Python is also used.

Which data frame method is used to remove a column from the resultant DataFrame?

Pandas DataFrame drop() Method The drop() method removes the specified row or column. By specifying the column axis ( axis='columns' ), the drop() method removes the specified column. By specifying the row axis ( axis='index' ), the drop() method removes the specified row.


1 Answers

[Edit:] Explanation of why negative string indices does not work:

-() is a function and the R developers say it can't be used on a character vector (and not just because negating a string doesn't make sense). Because you can't negate a character vector, you can't supply negative strings to drop columns. The problem is with - and is the source of the error message you quote. Hence the rule that negative indices only work for numerics. The source of the original error is:

> -"cyl" Error in -"cyl" : invalid argument to unary operator 

Note that in the comments to the Q, there was confusion that the negative version of "cyl" was "-cyl", which it isn't, it is just another string. The R snippet above shows what was happening in the subsetting tried in the Question.

Section 2.7 of the "An Introduction to R" manual describes the allowed methods of subsetting.

[Original:] The simplest way to remove a component is just to set that component to NULL:

> cars <- mtcars > cars[, "cyl"] <- NULL ## or cars$cyl <- NULL > names(cars)  [1] "mpg"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb" 

[Edit:] In light of the Edit to the Q indicating a temporary drop of the named column was desired, then:

subset(mtcars, select = -cyl) 

or

mtcars[, !names(mtcars) %in% "cyl"] 

are options, and the former cleaner than the latter.

like image 94
Gavin Simpson Avatar answered Sep 19 '22 00:09

Gavin Simpson