14

Sorry this seems like a really silly question but are dataframe[ ,-1] and dataframe[-1] the same, and does it work for all data types?

And why are they the same

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
CArnold
  • 465
  • 4
  • 7
  • 16

3 Answers3

29

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"
flodel
  • 87,577
  • 21
  • 185
  • 223
  • I was trying to put an example of a plot, but I'll just ask. Will a plot be affected by which you use. ie if you havent converted back into a dataframe – CArnold Jan 15 '14 at 12:35
  • Unlikely, but you could show us how you use `plot` to confirm. Also give use `dim(dataframe)`. – flodel Jan 15 '14 at 12:37
  • Thanks everybody. @flodel, I'll carry on trying to solve my plot problem. Will post again if I can't get it to work. – CArnold Jan 15 '14 at 12:40
  • One of the reasons Hadley created tibbles: no matter the dimensions, in either case, the result is a tibble. – prosoitos Oct 26 '18 at 08:16
3

dataframe[-1] will treat your data in vector form, thus returning all but the very first element [[edit]] which as has been pointed out, turns out to be a column, as a data.frame is a list. dataframe[,-1] will treat your data in matrix form, returning all but the first column.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • You need to remove the "No." dataframe is a vector, but not an atomic vector. Its first element is the first column. – Roland Jan 15 '14 at 12:33
  • each list element of the data.frame is a column, I don't think he said anything wrong. – flodel Jan 15 '14 at 12:35
1

Sorry, wanted to leave this as a comment but thought it was too big, I just found it interesting that the only one which remains a non integer is dataframe[1].

Further to Carl's answer, it seems dataframe[[1]] is treated as a matrix as well. But dataframe[1] isn't....

But it can't be treated as a matrix cause the results for dataframe[[1]] and matrix[[1]] are different.

D <- as.data.frame(matrix(1:16,4))
D
M <- (matrix(1:16,4))
M
> D[ ,1]        # data frame leaving out first column
[1] 1 2 3 4
> D[[1]]        # first column of dataframe
[1] 1 2 3 4
> D[1]          # First column of dataframe
  V1
1  1
2  2
3  3
4  4
> 
> class(D[ ,1])
[1] "integer"
> class(D[[1]])
[1] "integer"
> class(D[1])
[1] "data.frame"
> 
> M[ ,1]        # matrix leaving out first column
[1] 1 2 3 4
> M[[1]]        # First element of first row & col
[1] 1
> M[1]          # First element of first row & col
[1] 1
> 
> class(M[ ,1])
[1] "integer"
> class(M[[1]])
[1] "integer"
> class(M[1])
[1] "integer"
CArnold
  • 465
  • 4
  • 7
  • 16