Almost.
[-1]
uses the fact that a data.frame is a list, so when you do dataframe[-1]
it returns another data.frame (list) without the first element (i.e. column).
[ ,-1]
uses the fact that a data.frame is a two dimensional array, so when you do dataframe[, -1]
you get the sub-array that does not include the first column.
A priori, they sound like the same, but the second case also tries by default to reduce the dimension of the subarray it returns. So depending on the dimensions of your dataframe
you may get a data.frame or a vector, see for example:
> data <- data.frame(a = 1:2, b = 3:4)
> class(data[-1])
[1] "data.frame"
> class(data[, -1])
[1] "integer"
You can use drop = FALSE
to override that behavior:
> class(data[, -1, drop = FALSE])
[1] "data.frame"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…