Re: [R] Which data structure in R can mimic hash in PERL best?

From: Hans W. Borchers <hwborchers_at_googlemail.com>
Date: Sat, 15 Nov 2008 08:27:07 -0800 (PST)

> Dear all,
>
> Which data structure in R can mimic hash in PERL? I'd like to set
> up a lookup table, which could be accomplished by HASH if using PERL.
> Which of the data structures in R is the most efficient for lookup
> table?
> Thanks for your help.
>
> Best regards,
> Leon

The regular answer to this is "named arrays/vectors" or "environments" and has been given several times here on R-help. Unfortunately, everybody is applying a different terminology such as 'maps', 'hashes', 'tables' or 'dictionaries', etc., so it's difficult to search for those entries.

Below I enclose a solution that I have written some month ago. It is given in pythonic language, but you can easily translate it into the corresponding Perl terms. One drawback is that one cannot use numbers as keys, only keys following the naming conventions for variable names are allowed.

Hans Werner Borchers
ABB Corporate Research

#-- Define functions on Hash Tuples (Python alike) ---------------------

def.h <- function() new.env(hash=TRUE)
len.h <- function(dict) length(ls(envir=dict))
set.h <- function(key, val, dict) assign(key, val, envir=dict) get.h <- function(key, dict, default=NULL) {

    if (exists(key, envir=dict)) { get(key, dict)     } else { default }
}
has_key <- function(key, dict) exists(key, envir=dict) keys.h <- function(dict) ls(envir=dict)
items.h <- function(dict) as.list(dict)
values.h <- function(dict, mode='character') {

    l <- as.list(dict)
    n <- length(l)
    if (n==0) invisible(NULL)
    v <- vector('character', n)
    for (i in 1:n) v[i] <- l[[i]]
    if (mode=='numeric') v <- as.numeric(v)     return(v)
}
del.h <- function(key, dict) {

    if (exists(key, envir=dict)) {

        val <- get.h(key, dict)
        rm(list=c(key), envir=dict)
    } else {
        val <- NULL

    }
    invisible(val)
}
clear.h <- function(dict) {

    rm(list=keys.h(dict), envir=dict)
}

#-----------------------------------------------------------------------

-- 
View this message in context: http://www.nabble.com/Which-data-structure-in-R-can-mimic-hash-in-PERL-best--tp20515707p20517058.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sat 15 Nov 2008 - 16:34:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 18 Nov 2008 - 12:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive