New R package: a dictionary with arbitrary keys and values

March 11, 2016
By

(This article was first published on bioCS, and kindly contributed to R-bloggers)

Coming from Python, the absence of a real dictionary in R has annoyed me for quite some time. Now, I actually needed to use vectors as keys in R:

> library(dict)
> d <- dict="" font="">
> d[[1]] <- 42
> d[[c(2, 3)]] <- "Hello!"
> d[[“foo”]] <- "bar"
> d[[1]]
[1] 42
> d[[c(2, 3)]]
[1] “Hello!”

Under the hood, separate C++ dictionaries (unordered_map) are created for the different types of keys. Using R’s flexible types that are reflected in Rcpp (as SEXP), such a dictionary can store both numbers and strings (and other objects) at the same time.

The package is available on GitHub: https://github.com/mkuhn/dict

To leave a comment for the author, please follow the link and comment on their blog: bioCS.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)