Indexing a List with an Empty Index

The technique of indexing a data frame with an empty index has several times in Hadley Wickam Advanced R , but is explained there only along the way. I am 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:

  • Why is the output of the second operator different from the output of the third operator? Is the task not assigned to return the assigned object, in which case the second statement should give the same result as the third?
  • How did it happen that the assignment in the second expression led to the result shown after the third statement? What are the rules governing the assignment of a floating-index list?
  • How did it happen that the fourth operator showed the result? What are the rules governing indexing a list with an empty index if it is not on the left side of the job?
+8
list r indexing
source share
2 answers

In short, l[] will return the entire list.

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

l[] <- list(c=3) essentially reassigns what was assigned to each index will now be the result of list(c=3) . For this example, this is the same as saying l[[1]] <- 3 and l[[2]] <- 3 . On the page ?'[' , Which mentions empty indexing several times:

When the index expression appears on the left side of the assignment (the so-called auxiliary assignment), then this part x is set to the value on the right side of the assignment.

and

An empty index selects all values: this is most often used to replace all records, but retains attributes.

So, I roughly believe that every index l should be evaluated to list(c=3) .

When you enter (l[] <- list(c = 3)) return value is a replacement value. When you enter l or l[] , you will see that the values ​​in each index have been replaced by list(c=3) .

+2
source share

In addition to the previous answer, check this out. Note that the behavior is exactly the same as regular vectors and lists, so it cannot be called "list specific".

 v <- 1:3 names(v) <- c("one", "two", "three") r <- 4:5 names(r) <- c("four", "five") (v[] <- r) four five 4 5 Warning message: In v[] <- r : number of items to replace is not a multiple of replacement length v one two three 4 5 4 

Assignment through a subset saves the initial attributes (here, names). Thus, the names on the right side of the recipient are lost. Importantly, assignment through a subset follows disposal rules. In your example, all values ​​are reassigned to 3, in my example, partial recirculation occurs with a warning due to incompatibility in length.

Summarizing,

  • Assigning with <- returns the estimated right side to , using the disposal rules.
  • This is due to disposal , since the lengths of the two objects are different.
  • Without the assignment operator, l or v essentially matches l[] or v[] .
+2
source share

All Articles