What prevents us from implementing traditional imperative-language data structures in R is the immutability of data. As a general rule, you can modify environments—so you can assign to variables—but you cannot modify actual data. Whenever R makes it look like you are changing data, it is lying. When you assign to an element in a vector
x[i] <- v
the vector will look modified to you, but behind the curtain, R has really replaced the vector that x refers to with a new copy, identical to the old x except for element number i. It tries to do this efficiently, so it will ...