I am trying to remove some columns in a dataframe. I want to know why it worked for a single column but not with multible columns e.g. this works
album2[,5]
Adding answer as this was the top hit when searching for "drop multiple columns in r":
The general version of the single column removal, e.g df$column1 <- NULL, is to use list(NULL):
df[ ,c('column1', 'column2')] <- list(NULL)
This also works for position index as well:
df[ ,c(1,2)] <- list(NULL)
This is a more general drop and as some comments have mentioned, removing by indices isn't recommended. Plus the familiar negative subset (used in other answers) doesn't work for columns given as strings:
> iris[ ,-c("Species")]
Error in -"Species" : invalid argument to unary operator