3

I really wasn't sure how to ask this question so I'm sorry if it's not clear.

The thing is, by accident I stumbled upon a solution to one of my problems, that is how to extract all the 1st columns from a list of objects. The solution I found was

lapply(lst, "[", 1, )

and it works perfectly, but I can't seem to figure out what this part means "[", 1,. Can someone please explain it to me or at least give me some literature on it. Tnx

BStat
  • 41
  • 7
  • `lapply` is looping through each element of your list and applying a function. In this case, the `"[" `part is saying apply the bracket function to each element of the list, while the `1` part is an argument passed to the bracket, saying extract the first element. See ?"[" for more info on the bracket function. – Daniel Anderson Oct 27 '16 at 18:48
  • 1
    `list[index]` is the same as `"["(list, index)` – blmoore Oct 27 '16 at 18:49
  • [This short explanation](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24375303#24375303) might help. – Rich Scriven Oct 27 '16 at 18:52

1 Answers1

2

"[" is the function you're applying to all objects in the list (see ?"[" for more about this function). This function extracts parts of an object. 1 is the argument you're passing to the function, so that "[" extracts the first element in each object.

yeedle
  • 4,918
  • 1
  • 22
  • 22