title: Understanding list indexing and bracket conventions in R
body: <p>I almost understand list indices in R, but I have a few lingering questions.  Specifically I am trying to understand using multiple indices to get to different layers of data in a list, and the types of brackets to use at each level with each data type. First I will show a simple example list and my understanding so far.</p>
  
  <pre><code>#Make an example list "L" containing different types of data
  x&lt;- matrix(1:12,3,4)
  y&lt;- seq(2,17,3)
  z&lt;- list(letters[1:5],LETTERS[1:5],letters[6:10],LETTERS[6:10])
  L&lt;- list(x,y,z)
  </code></pre>
  
  <p>Top level of indexing:
  L is a list containing 3 elements, x, y, and z.  To see an element in a list you use double brackets.  </p>
  
  <pre><code>#see matrix x, the first element of list L
  x
  L[[1]]
  </code></pre>
  
  <p>Second level of indexing:
  Simply add the indices for whatever data type the element is after the double brackets specifying that element.</p>
  
  <pre><code>#List L's first element is matrix x, see x's row 2 column 3 value:
  x[2,3]
  L[[1]][2,3]
  #List L's second element is vector y, see y's 4th value:
  y[4]
  L[[2]][4]
  #List L's third element is list z, see z's first element:
  z[[1]]
  L[[3]][[1]]
  </code></pre>
  
  <p>3rd level of indexing: just continue adding brackets for each level. </p>
  
  <pre><code>#List L's third element is list z, list z's first element is vector letters[1:5],
  #see list z's first element's fifth value:
  letters[1:5][5]
  z[[1]][5]
  L[[3]][[1]][5]
  </code></pre>
  
  <p>OK.  So that is all straightforward enough.  You use [[element]] for lists and [row,column] for matrices and [index] for vectors.  My questions are about when you DON'T.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>If you use a double bracket for matrices and vectors it still works.  <strong>So is there any difference between the single and double bracket for matrices and vectors?</strong></p></li>
  <li><p>If you use a single bracket with a list, it sort of still works.  But instead of returning (as with [[1]]) the first element as that element, [1] returns a list that contains element 1.  So you could use list[c(elements)] to get a list with a subset of elements.  <strong>Is there any other reason you would ever use single brackets with a list?</strong></p></li>
  <li><p>I did not really touch on dataframes here, which are a type of list.  But they are shaped like matrices and you can also see values using [row,column] as with matrices.  <strong>Is there any "danger" in using the [row,column] indexing with dataframes?</strong></p></li>
  </ol>
  
  <p>note: previous discussions were very helpful, but did not answer these specific questions: <a href="https://stackoverflow.com/questions/2050790/how-to-correctly-use-lists-in-r">How to Correctly Use Lists in R?</a>
  <a href="https://stackoverflow.com/questions/1169456/in-r-what-is-the-difference-between-the-and-notations-for-accessing-the/1169973#1169973">The difference between [] and [[]] notations for accessing the elements of a list or dataframe</a></p>