0
name

[[1]]

[1] "John"  "Davis"

[[2]]

[1] "Angela"   "Williams"

[[3]]

[1] "Bullwinkle" "Moose" 

The data is as above, I want to take last and first name from the list. The code is:

lastname <- sapply(name, "[", 2)

My question: what does the [ mean?

Sotos
  • 51,121
  • 6
  • 32
  • 66
Tina
  • 9
  • 1

1 Answers1

1

It is ?Extraction operator. Here, it extracts the 2nd element of the list.

sapply(name, `[`, 2)

In the OP's post, the list elements are vectors. So, it checks the 2nd element and extract that element and output as a vector (sapply)

akrun
  • 874,273
  • 37
  • 540
  • 662