data structure to check if list contains an element in constant time in R
I am looking for data structure to check if a name is contained in a list in constant time. Currently I am doing this, but it probably takes O(N) complexity.
> l = read.csv("test.csv")[,1] # reads a开发者_开发技巧 long list of names
> if (x %in% l) { ... } # check if it contains a name, takes O(N) time
You could use the hash
package which provides associative arrays. You'll have to spend some time building the hash but it will pay off when you use the has.key() method which should have O(1) lookup speed.
CRAN has what you need.
You can also use environments as an implemented hash table. See contributions to this thread from 6 years ago:
http://finzi.psych.upenn.edu/R/Rhelp02/archive/51512.html
精彩评论