In R semantics, objects are copied by value. This means that modifying the copy leaves the original object intact. Since, copying data in memory is an expensive operation, copies in R are as lazy as possible. They only happen when the new object is actually modified. However, some operations (like mut_node_car() or mut_node_cdr()) do not support copy-on-write. In those cases, it is necessary to duplicate the object manually in order to preserve copy-by-value semantics.

duplicate(x, shallow = FALSE)



Any R object. However, uncopyable types like symbols and environments are returned as is (just like with <-).


This is relevant for recursive data structures like lists, calls and pairlists. A shallow copy only duplicates the top-level data structure. The objects contained in the list are still the same.


Some objects are not duplicable, like symbols and environments. duplicate() returns its input for these unique objects.

See also