13
  > levels(state.region)[2]
    [1] "S"
    > levels(state.region)[[2]]
    [1] "S"

They return the same value, so I don't know what is the difference between them.

statistics_learning
  • 427
  • 1
  • 4
  • 14

1 Answers1

13

[] = always returns object of same class (out of basic object classes), can select more than one element of an object

[[]] = can extract one element from list or data frame, returned object (out of basic object classes) not necessarily list/dataframe

sdayal
  • 1,126
  • 10
  • 16
  • 6
    `[[` can extract from more that just a list or data.frame. It also works with vectors, matrices, environments, etc. – Joshua Ulrich Nov 01 '15 at 13:57
  • 1
    very late here... but what use is `[[` when applied to a vector or matrix? I can't think of any examples where it differs from `[` (just like the OP's question demonstrates). – RTbecard May 31 '21 at 14:34