Jamesm131
Jamesm131

Reputation: 155

Syntax of sapply and lapply in r

I have been looking into ways to separate a list in r based on the index of the sub values and I have seen this piece of code pop up a lot:

sapply(myList, "[", 2)

I was wondering if anyone would be able to explain this to me as the only syntax that I have used for sapply before is:

sapply(myList, Function)

Upvotes: 2

Views: 379

Answers (1)

joel.wilson
joel.wilson

Reputation: 8413

"[" is by itself a function.

when you call sapply(LIST, "[", 2) it means extract the second element of each sub-list.

The 2 which you passed goes as an argument to the function [ making it [2]

Upvotes: 2

Related Questions