3

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.

#Make an example list "L" containing different types of data
x<- matrix(1:12,3,4)
y<- seq(2,17,3)
z<- list(letters[1:5],LETTERS[1:5],letters[6:10],LETTERS[6:10])
L<- list(x,y,z)

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.

#see matrix x, the first element of list L
x
L[[1]]

Second level of indexing: Simply add the indices for whatever data type the element is after the double brackets specifying that element.

#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]]

3rd level of indexing: just continue adding brackets for each level.

#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]

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.

Questions:

  1. If you use a double bracket for matrices and vectors it still works. So is there any difference between the single and double bracket for matrices and vectors?

  2. 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. Is there any other reason you would ever use single brackets with a list?

  3. 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. Is there any "danger" in using the [row,column] indexing with dataframes?

note: previous discussions were very helpful, but did not answer these specific questions: How to Correctly Use Lists in R? The difference between [] and [[]] notations for accessing the elements of a list or dataframe

Community
  • 1
  • 1
rrr
  • 1,173
  • 1
  • 15
  • 22
  • 2
    1) The most important distinction between `[`, `[[` and `$` is that the `[` can select more than one element whereas the other two select a single element; 2) yes, if you want the entire first element of a list which could be a single integer or a huge data frame; 3) Danger? I think `[x, y]` where x and y are integers, is the preferred way to index data sets. If you use a string, for example, and index `df[, 'a']` to get column "a," it would cause problems if you forget the quotes and have `a` in your workspace as some random number. if you know the column/row index, `[x,y]`is always safer – rawr Mar 16 '14 at 00:29
  • 1
    Have a look at http://adv-r.had.co.nz/Subsetting.html – hadley Mar 16 '14 at 15:40
  • @hadley thanks for the great link! – rrr Mar 17 '14 at 03:01
  • @rawr thanks! if you put that as an answer instead of a comment I will accept it :) However, I'm not sure I understand your 2) explanation. `[[ ]]` will pull out the whole element of a list, see in my example `L[[3]][[1]]`. I don't understand the distinction you're making- can you give an example? Thanks! – rrr Mar 17 '14 at 03:06

0 Answers0