Data Frame as Hash Table

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Data Frame as Hash Table

Alan Lue
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Data Frame as Hash Table

Barry Rowlingson
On Sun, May 30, 2010 at 9:03 AM, Alan Lue <[hidden email]> 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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Data Frame as Hash Table

Patrick Burns
In reply to this post by Alan Lue
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
>
> ______________________________________________
> [hidden email] 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
[hidden email]
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Data Frame as Hash Table

Marsh
In reply to this post by Alan Lue
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, [hidden email] wrote:

> Message: 40
> Date: Sun, 30 May 2010 09:24:22 +0100
> From: Patrick Burns<[hidden email]>
> To:[hidden email],[hidden email]
> Subject: Re: [R] Data Frame as Hash Table
> Message-ID:<[hidden email]>
> 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
>> >
>> >  ______________________________________________
>> >  [hidden email]  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 [hidden email] http://www.burns-stat.com 
> (home of 'Some hints for the R beginner' and 'The R Inferno')

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Data Frame as Hash Table

Alan Lue
Thanks, guys!

Alan


On Sun, May 30, 2010 at 5:35 AM, Marshall Feldman <[hidden email]> wrote:

> 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, [hidden email] wrote:
>>
>> Message: 40
>> Date: Sun, 30 May 2010 09:24:22 +0100
>> From: Patrick Burns<[hidden email]>
>> To:[hidden email],[hidden email]
>> Subject: Re: [R] Data Frame as Hash Table
>> Message-ID:<[hidden email]>
>> 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
>>> >
>>> >  ______________________________________________
>>> >  [hidden email]  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 [hidden email] http://www.burns-stat.com (home
>> of 'Some hints for the R beginner' and 'The R Inferno')
>
>



--
Alan Lue
Master of Financial Engineering
UCLA Anderson School of Management

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Data Frame as Hash Table

Don MacQueen
In reply to this post by Alan Lue
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
>
>______________________________________________
>[hidden email] 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
[hidden email]

______________________________________________
[hidden email] 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.