I have looked this question and this question, but neither seems to address the problem of checking for the existence of an integer key in a list.
Consider the following example.
mylist=list()
mylist[[5]] = 1
# How do I programmatically check whether 5 is a key for this list?
5 %in% names(mylist) # returns FALSE, because names(mylist) is NULL here.
Update: Clarification, using another language, like Python. Here is the behavior I am trying to replicate in R.
foo = {}
foo[5] = 1
if 5 in foo: # How do I say "if 5 in foo" in R?
print foo[5]
# Do other stuff
Rlists to became at least somewhat like dictionaries / hashmaps / associative arrays in other languages. Is this an incorrect premise?5 %in% seq_along(mylist)is what you want. This checks thatmylistcontains a 5th element. Alsois.null(mylist[[5]])might help4 in seq_along(mylist)also returnsTRUE, unfortunately.