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? Yours, Alan Lue
On Sun, May 30, 2010 at 9:03 AM, Alan Lue <alan.lue at 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 > dkey 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
You might want to investigate the 'data.table' package. On 30/05/2010 09:03, Alan Lue 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? > > Yours, > Alan Lue > > ______________________________________________ > 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. >-- Patrick Burns pburns at pburns.seanet.com http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
Besides data.table, there's the hash package. It does not use data.frame type structures but is a bit more flexible. Marsh Feldman On 5/30/10 [May 30, 10] 6:00 AM, r-help-request at r-project.org wrote:> Message: 40 > Date: Sun, 30 May 2010 09:24:22 +0100 > From: Patrick Burns<pburns at pburns.seanet.com> > To:r-help at r-project.org,alan.lue at gmail.com > Subject: Re: [R] Data Frame as Hash Table > Message-ID:<4C0220B6.7090006 at pburns.seanet.com> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > You might want to investigate the 'data.table' > package. > > On 30/05/2010 09:03, Alan Lue 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? >> > >> > Yours, >> > Alan Lue >> > >> > ______________________________________________ >> > R-help at r-project.org mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> > >> > -- Patrick Burns pburns at pburns.seanet.com http://www.burns-stat.com > (home of 'Some hints for the R beginner' and 'The R Inferno')
If you only need a single variable (in this case value), and just want to refer to it by the "key", there are other options. value <- rnorm(6) names(value) <- format(seq(0.5,3,0.5)) value['1.5'] But do watch out for numerical precision in the output of seq() if your vector of values is long. Or, given the dataframe version, it's not essential to assign key to the row names d[ d$key==1.5 , ] or subset(d , key==1.5) (again with some potential for numerical precision issues) If you want your psuedo-hash-table to reference more complex structures, use a list. myhash <- vector('list',6) ## initialize a list of six elements names(myhash) <- letters[1:6] ## name the six elements myhash$a <- data.frame(x=1:4, y=c('a','b','d','f')) ## assign something to the first element myhash$b <- rnorm(10) ## assign something to the second element and so on for $c, $d, $e, and $f .... the elements don't even have to have the same structure -Don At 1:03 AM -0700 5/30/10, Alan Lue 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? > >Yours, >Alan Lue > >______________________________________________ >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.-- --------------------------------- Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 macq at llnl.gov