Understanding list indexing and bracket conventions in R

萝らか妹 提交于 2019-12-01 07:03:59

问题


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

来源:https://stackoverflow.com/questions/22431261/understanding-list-indexing-and-bracket-conventions-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!