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?
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.
The most common way to remove a column is using df. drop() . Sometimes, del command in Python is also used.
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.
[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.
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