How to remove columns in with subset function?

Viewed 33

Could you please have a look on this code?

cereals<- subset(cereals, select=-c("name",
"rating",
"cups",
"weight",
"type",
"fat",
"sodium",
"fiber",
"carbo",
"potass"))

My excel file does have this columns and I need to remove those in R, however it pops up with the error message below:

Error in -c("name", "rating", "cups", "weight", "type", "fat", "sodium",  : 
  invalid argument to unary operator

Would appreciate your help

1 Answers

The quotes are superfluous.

subset(mtcars[1:5, ], select=-c('cyl', 'disp', 'drat'))  ## fails
# Error in -c("cyl", "disp", "drat") : invalid argument to unary operator

subset(mtcars[1:5, ], select=-c(cyl, disp, drat))  ## works
#                    mpg  hp    wt  qsec vs am gear carb
# Mazda RX4         21.0 110 2.620 16.46  0  1    4    4
# Mazda RX4 Wag     21.0 110 2.875 17.02  0  1    4    4
# Datsun 710        22.8  93 2.320 18.61  1  1    4    1
# Hornet 4 Drive    21.4 110 3.215 19.44  1  0    3    1
# Hornet Sportabout 18.7 175 3.440 17.02  0  0    3    2
Related