Are dataframe[ ,-1] and dataframe[-1] the same?

后端 未结 3 1062
夕颜
夕颜 2020-12-16 15:56

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

3条回答
  •  春和景丽
    2020-12-16 16:16

    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"
    

提交回复
热议问题