Ramda.js R.countBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Relation

Definition

Ramda’s R.countBy() answers a tally question: how many list items share each key from this function? You pass a key function, then a list. You get back a plain object: each key is String(fn(item)), each value is how many times that key appeared. Need the items themselves, not the counts? That is R.groupBy (List category). Need one item per key? That is R.indexBy. Need a custom fold per key? That is R.reduceBy. Lodash _.countBy and Underscore _.countBy are the same histogram idea. Ramda is data-last and automatically curried, so R.countBy(Math.floor) is already a function of one list.

The official samples are the whole idea. Floor a mix of floats and you get {'1': 3, '2': 2, '3': 1}. Lowercase a mix of letters and you get {'a': 3, 'b': 2, 'c': 1}. Previous helper is R.clamp. Next is R.difference.

Syntax

Call R.countBy. Start with this line.

Ramda
R.countBy(fn, list)
CallWhat it does
R.countBy(fn, list)Primary signature.
R.countBy(...)Call the relation helper.

Prefer import { countBy } from "ramda" when you want a named import.

Library declarations
Ramda
R.countBy(fn, list)

Parameters

ParameterDescription
fnRequired. a → String. The function used to map values to keys. Result is coerced to a string.
listRequired. Array (or transformer). The list to count elements from. Not mutated. A transformer in this position makes countBy act as a transducer.

Return Value

Return ValueDescription
ObjectAn object mapping keys produced by fn to the number of occurrences. All keys are strings. Empty list → {}.

Ramda Examples

📚 Getting Started

The two official Ramda samples: floor a list of numbers, then lowercase a list of letters.

Example 1 — Official Number Sample

Each float is keyed by Math.floor. Values that share a floor increment the same count.

Ramda
var numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
R.countBy(Math.floor)(numbers);
//=> {"1": 3, "2": 2, "3": 1}
Try It Yourself

How It Works

That is the docs sample from ramdajs.com. 1.0, 1.1, and 1.2 floor to 1. 2.0 and 2.2 floor to 2. 3.0 floors to 3. The keys on the object are the strings '1', '2', and '3'.

Example 2 — Official Letter Sample

R.toLower folds case first, so 'A' and 'a' share one bucket.

Ramda
var letters = ['a', 'b', 'A', 'a', 'B', 'c'];
R.countBy(R.toLower)(letters);
//=> {"a": 3, "b": 2, "c": 1}
Try It Yourself

How It Works

Three items become 'a' ('a', 'A', 'a'). Two become 'b'. One becomes 'c'. Previous helper is R.clamp.

📈 Practical Patterns

Same key function as groupBy, then object props, then the string-key and empty-list notes.

Example 3 — vs R.groupBy

The same Math.floor key. countBy stores counts. groupBy stores arrays of the original numbers.

Ramda
var numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];

R.countBy(Math.floor, numbers);
//=> {"1": 3, "2": 2, "3": 1}

R.groupBy(Math.floor, numbers);
//=> {"1": [1, 1.1, 1.2], "2": [2, 2.2], "3": [3]}
Try It Yourself

How It Works

Keys match. Payloads do not. If you only needed .length of each group, skip groupBy and stay with countBy. Full walkthrough: R.groupBy.

Example 4 — Object Prop Keys with R.prop

Tally users by role. R.prop('role') is the key function.

Ramda
var users = [
  { role: 'admin' },
  { role: 'user' },
  { role: 'admin' },
  { role: 'guest' },
  { role: 'user' }
];

R.countBy(R.prop('role'), users);
//=> {"admin": 2, "user": 2, "guest": 1}
Try It Yourself

How It Works

Because countBy is arity 2, filling the key function leaves a leftover for a later list: R.countBy(R.prop('role')). Lodash would write _.countBy(users, 'role') — collection first, property shorthand second.

Example 5 — String Keys and Empty List

Math.floor returns numbers, but object keys become '1' and '2'. An empty list has nothing to count, so the result is {}.

Ramda
var counts = R.countBy(Math.floor, [1.0, 1.1, 2.0]);
Object.keys(counts);
//=> ["1", "2"]

typeof Object.keys(counts)[0];
//=> "string"

R.countBy(Math.floor, []);
//=> {}
Try It Yourself

How It Works

JavaScript objects coerce keys to strings. The type signature writes a → String for that reason. No items means no keys, so you get a fresh empty object, not null or undefined.

Browser Support

R.countBy() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.countBy()

Load Ramda 0.1.0 or later via CDN or npm, then call R.countBy with a key function and a list. No extra browser API is required.

100% With Ramda 0.1.0+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
R.countBy() Universal

Bottom line: Safe in any project that includes Ramda 0.1.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].

Frequently Asked Questions

R.countBy(fn, list) counts list elements by keys produced by fn. The result is an object of key → count. Type: (a → String) → [a] → {*}. Arity 2, curried. Added in v0.1.0.
R.countBy(Math.floor)([1.0, 1.1, 1.2, 2.0, 3.0, 2.2]) is {'1': 3, '2': 2, '3': 1}. R.countBy(R.toLower)(['a', 'b', 'A', 'a', 'B', 'c']) is {'a': 3, 'b': 2, 'c': 1}.
Same key function. countBy stores how many items matched each key (numbers). groupBy stores the items themselves (arrays). indexBy keeps one item per key. reduceBy lets you fold a custom accumulator per key. Lodash _.countBy and Underscore _.countBy are the same idea with iteratee-first or list-first argument order.
The result is a plain JavaScript object. Object keys are always strings (or symbols). Math.floor(1.2) is the number 1, but the key becomes '1'.
An empty object {}. There are no elements, so there are no keys.
No. R.countBy is a stable Ramda relation helper from v0.1.0. It is not deprecated, experimental, or non-standard. It also acts as a transducer if a transformer is given in list position.

Continue to R.difference()

First list minus second: unique leftovers compared with R.equals.

R.difference() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful