Why subset doesn't work with string column names when removing columns?

Viewed 27

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?

0 Answers
Related