Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding named item to named list - guaranteed to append to end of list?

When adding a named item to a list, is it guaranteed that the item will be added to the end of the list? In practice it appears to be the case, but not sure if this is a dangerous assumption?

test = list()
test[[ "one" ]] = 1
test[[ "two" ]] = 2  # will always appear after "one"?
test[[ "three" ]] = 3  # will always appear after "two"?
like image 263
SFun28 Avatar asked Sep 29 '11 15:09

SFun28


2 Answers

If it's not documented (and it doesn't appear to be), then I wouldn't rely on it. You can ensure it appears at the end of the list by doing something like:

test <- list()
test <- c(test, one=1)
test <- c(test, two=2)
test <- c(test, three=3)
like image 198
Joshua Ulrich Avatar answered Oct 24 '22 14:10

Joshua Ulrich


I suspect if you delved into the C code of R then you'd see that it was true, but as Joshua says, its not documented. You could ask on R-dev for an opinion on whether such behaviour should be documented. There may already be existing code that depends on it.

like image 5
Spacedman Avatar answered Oct 24 '22 14:10

Spacedman