Skip to content

Instantly share code, notes, and snippets.

@lcustodio
Created December 1, 2019 21:49
Show Gist options
  • Save lcustodio/4c9681423488f68265bde8340dfb5a1d to your computer and use it in GitHub Desktop.
Save lcustodio/4c9681423488f68265bde8340dfb5a1d to your computer and use it in GitHub Desktop.
R Hashmap
# https://rnotebook.io/anon/ae2ac79d0d10d6f6/notebooks/hashmap.ipynb
library(hash)
h <- hash()
a <- matrix(1:4, nrow = 12, ncol = 3)
print(a)
# has.key("4",h)
hashPopulator <- function(key) {
# print("this is the key")
# print(key)
index <- a[key, 1]
if( has.key(toString(key), h ) ) {
# print('key present')
h[index] <- rbind(h[[toString(index)]], matrix(a[key,]))
} else {
# print('false')
h[index] <- matrix(a[key,])
}
}
hashPopulator(a[4,1]);
hashPopulator(a[5,1]);
hashPopulator(a[8,1]);
hashPopulator(a[11,1]);
hashPopulator(a[12,1]);
print(values(h))
# print(h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment