On Sun, May 30, 2010 at 9:03 AM, Alan Lue <alan....@gmail.com> wrote:
> I'm interested in using a data frame as if it were a hash table.  For
> instance if I had the following,
>
>> (d <- data.frame(key=seq(0.5, 3, 0.5), value=rnorm(6)))
>  key        value
> 1 0.5 -1.118665122
> 2 1.0  0.465122921
> 3 1.5 -0.529239211
> 4 2.0 -0.147324638
> 5 2.5 -1.531503795
> 6 3.0 -0.002720434
>
> Then I'd like to be able to quickly retrieve the "value" of "key" 1.5
> to get -0.53.  How would one go about doing this?

Assign the key to the rownames:

> row.names(d)=d$key
> d
    key      value
0.5 0.5 -0.1023732
1   1.0 -0.2005591
1.5 1.5  0.1204866

but note they are character strings:

> d["0.5",]
    key      value
0.5 0.5 -0.1023732

 I'm not sure if R uses a fast hashing algorithm for lookups or a
simple sequential search. Looking at the source code, testing, or
waiting for someone else to answer that on here will tell.

 Or you could do it with a list, but again the keys are always
character strings.

Barry

______________________________________________
R-help@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.

Reply via email to