Let's say, we have a simple data frame like
df <-read.table(text="
colA colB colC colD
1 2 3 4
5 6 7 8
",header=TRUE,sep="")
It has often been explained that one can store the names of columns to be kept in a vector itself:
rows_to_select <- c("colA", "colB")
Subsetting with subset(df, select=rows_to_select)
yields the expected outcome.
But why can't I simply invert the keep-sign by putting a minus in front, i.e. subset(df, select=-rows_to_select)
? It gives the error Error in -keep : invalid argument to unary operator Calls: subset -> subset.data.frame -> eval -> eval
.
However, subset(df, select=-c(colA, colB))
works. Do I always have to employ setdiff, e.g. keep <- setdiff(names(df), rows_to_select)
so that I can subset(df, select=keep)
?