Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of `lapply(lst, "[", 1, )` [duplicate]

Tags:

r

lapply

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

like image 863
BStat Avatar asked Dec 14 '25 07:12

BStat


1 Answers

"[" 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.

like image 80
yeedle Avatar answered Dec 16 '25 00:12

yeedle