0

Consider the following dataframe:

df <- data.frame(x=1:5, y=6:10, z=11:15, a=16:20)

I can select a column using just the name:

df2 <- subset (df, select = x)

Or I can select it passing the name as a string:

df2 <- subset (df, select = c("x"))

I can delete a column by passing just the name:

df2 <- subset (df, select = -x)

But I cannot delete it passing the name as string:

df2 <- subset (df, select = -c("x"))

As it returns the error:

Error in -c("x") : invalid argument to unary operator

Why?

robertspierre
  • 3,218
  • 2
  • 31
  • 46
  • 1
    According to `?subset` `select - expression, indicating columns to select from a data frame` i.e. try `subset (df, select = setdiff(names(df), c("x")))` – akrun Jul 12 '22 at 18:26
  • 1
    The `select=` parameter basically is evaluated within an environment where the column names are turned into their respective indexes. So like `with(setNames(as.list(seq_along(df)), names(df)), -x)` So you can use `-` with symbols because those are turning into numbers. But you can't use `-` with strings ever. You can see how this works if you look at the source of the function by typing `subset.data.frame` without the parens at the console. – MrFlick Jul 12 '22 at 18:41

0 Answers0