3
rquote <- "r's internals are irrefutably intriguing"
chars <- strsplit(rquote, split = "")[[1]] 

in the above line of code, what's the meaning of [[1]] ?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Hareesh
  • 31
  • 1

1 Answers1

2

The enforced [[1]] is a price you pay for generality. Consider this:

R> rquote <- c("R is cool", "But makes you wonder")
R> chars <- strsplit(rquote, split = "")
R> str(chars)
List of 2
 $ : chr [1:9] "R" " " "i" "s" ...
 $ : chr [1:20] "B" "u" "t" " " ...
R> 

So for input that is more than just one object, we get a list back with one list element per input. And for consistency it is the same on a single input: we get a list containing only one element. And [[1]] picks up that element.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725