---
title: "Discussion on \"Hash tables in R\" | Hashnode"
description: "Discussion on \"Hash tables in R\". And an old Quant UX interview question"
url: "https://hashnode.com/posts/hash-tables-in-r/64f4cd3b8381fda8931f8fa8"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Hash tables in R](https://quantuxblog.com/hash-tables-in-r?utm_source=hashnode&utm_medium=feed)

And an old Quant UX interview question

Sep 3, 2023 · 10 min read · quantuxblog.com  
Updated May 23, 2025  
Read the article

-   [#r](https://hashnode.com/tag/r)
-   [#quantux](https://hashnode.com/tag/quantux)

## Written by

[Chris Chapman](https://hashnode.com/@chrischapman)

@chrischapman · Seattle

  
[View profile](https://hashnode.com/@chrischapman)

R, Quant UX, and Marketing Research author. Executive Director, Quant UX Association.

President, Quant UX Association. Previously: Principal UX Researcher @ Google; Amazon Lab 126; Microsoft. Author of "Quantitative User Experience Research" and "\[R | Python\] for Marketing Research and Analytics".

## Responses 2

Markdown supported · ⌘↵ to post

[Riccardo Melani](https://hashnode.com/@RickyM)

[Sep 7, 2023](https://hashnode.com/posts/hash-tables-in-r/64f4cd3b8381fda8931f8fa8/comment/64fa3c56139d4c9cb61189fa)

Hello and thanks for this amazing blog! Assuming we have that character vector (**words**) containing the words to count (which would need to be generated following some text mining I guess?), how about this in tidyverse: wordcounter <- data.frame(word= words) %>% count(word)

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1694121024536/b7257799-19f1-4f55-b0f2-229925b73dfc.png)

[Chris Chapman](https://hashnode.com/@chrischapman)

[Sep 7, 2023](https://hashnode.com/posts/hash-tables-in-r/64f4cd3b8381fda8931f8fa8/comment/64fa3c56139d4c9cb61189fa#reply-64fa48c7c035cd988c325a4f)

Good question and there are many ways to solve the word counting problem! I didn't intend so much to solve it as to demonstrate hash tables.

As for a tidyverse solution, the thing I would watch out for — both in practice and in an interview — is the scalability and performance. As I linked in the previous comment thread, hash tables have nearly constant time needed per-element, regardless of the number of elements. Data frames, lists, and many other kinds of solutions require more and more time to look things up as the object gets larger.

For many applications the performance differences don't matter. However, in an interview situation, I would expect follow-up questions about the pros and cons of any particular solution.

For tidyverse, I might ask about the footprint size and the multi-year code stability of a solution used in production. (The goal of a question like that is to probe how much a candidate understands about the tradeoffs of code. We say a lot more about that kind of question in the book.)

FWIW, the least complex solution to the specific problem in R given the assumption of a "words" vector is: table(words).

However, for that answer, I would do exactly what my interview did at the time, and say, "OK, but assume you have to do this in base R and there is no table() \[or tidyverse\] function!"

[Jeremy Miles](https://hashnode.com/@jeremymiles)

[Sep 4, 2023](https://hashnode.com/posts/hash-tables-in-r/64f4cd3b8381fda8931f8fa8/comment/64f66ef0f6310e9375f96b86)

Is there a reason you prefer to use a new.env() rather than a list (which is how I would have done it)?

[Chris Chapman](https://hashnode.com/@chrischapman)

[Sep 5, 2023](https://hashnode.com/posts/hash-tables-in-r/64f4cd3b8381fda8931f8fa8/comment/64f66ef0f6310e9375f96b86#reply-64f68b97f6310e9375f96cb7)

Great question, and good to hear from you! Lists and environments are extremely similar (thanks to the functional, Scheme-like nature of R) so either should be possible. If I get a chance, I'll see how much difference appears when written with lists.

IMO environments are a slightly better match for hash tables because they are exactly hashed in their implementation and unordered (whereas lists have sequential elements) and they work by reference (instead of copying on change, thus can be performance enhancing). I'm not sure whether there are different scale issues vs. lists, but wouldn't expect any (because of the underlying Scheme-like nature, as above).

Still, the old joke about economists applies to most things in R: if you ask a question of 4 R experts, you'll get 5 different answers :)

[Chris Chapman](https://hashnode.com/@chrischapman)

[Sep 5, 2023](https://hashnode.com/posts/hash-tables-in-r/64f4cd3b8381fda8931f8fa8/comment/64f66ef0f6310e9375f96b86#reply-64f7946bc035cd988c3210ed)

Looking to see if anyone has compared these, I found that Jeffrey Horner has: [jeffreyhorner.tumblr.com/post/114524915928/hash-t…](https://jeffreyhorner.tumblr.com/post/114524915928/hash-table-performance-in-r-part-i)

The takeaway from that is that environments (using their default behavior of being hash tables under the hood) are substantially more efficient.

Environments/hashes have nearly constant speed (close to O(1)) as the size grows, but lists take linearly increasing time (close to O(n)) for lookups.
