Indexing a list with an empty index

Viewed 418

The technique of indexing a data frame with an empty index features several times in Hadley Wickam's Advanced R, but is only explained there in passing. I'm trying to figure out the rules governing indexing a list with an empty index. Consider the following four statements.

> (l <- list(a = 1, b = 2))
$a
[1] 1

$b
[1] 2

> (l[] <- list(c = 3))
$c
[1] 3

> l
$a
[1] 3

$b
[1] 3

> l[]
$a
[1] 3

$b
[1] 3

Questions:

  1. Why is the output from second statement different from the output from the third statement? Isn't assignment supposed to return the object being assigned to, in which case the second statement should yield the same output as the third one?
  2. How come did the assignment in the second statement result in the output shown after the third statement? What are the rules governing assignment to an emptily indexed list?
  3. How come did the fourth statement yield the output shown? What are the rules governing indexing a list with an empty index when it is not on the left hand side of an assignment?
2 Answers
Related