# Better way to determine most common difference of two columns of an RDataFrame

**URL:** <https://root-forum.cern.ch/t/better-way-to-determine-most-common-difference-of-two-columns-of-an-rdataframe/46996>\
**Category:** Newbie\
**Created:** [September 26, 2021, 4:10am UTC](https://root-forum.cern.ch/t/better-way-to-determine-most-common-difference-of-two-columns-of-an-rdataframe/46996 "2021-09-26T04:10:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KAM](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kam/32/37110_2.png) [@KAM](https://root-forum.cern.ch/u/KAM)\
**Post date:** [September 26, 2021, 4:10am UTC](https://root-forum.cern.ch/t/better-way-to-determine-most-common-difference-of-two-columns-of-an-rdataframe/46996/1 "2021-09-26T04:10:49Z")

</div>

I have an `RDataFrame`, which consists of three columns (`A`, `B`, and `run`). Here’s what it might look like in tabular form:

```auto
+-----+-----+-----+
| run | A | B |
+-----+-----+-----+
| 001 | 35 | 5 |
| 001 | 40 | 10 |
| 001 | 77 | 60 |
| | | |
| ... | ... | ... |
| | | |
| 002 | 42 | 40 |
| 002 | 30 | 28 |
| 002 | 50 | 1 |
| ... | ... | ... |
+-----+-----+-----+

```

_(where the … dots indicate continuation)_

For each run, I want to determine the most common difference of `A` and `B` (i.e., the mode of the set of entry-wise differences of `A` and `B` for each run). So, for example, for run `002` we have `42-40 = 2`, `30-28=2`, and `50 - 1=49`, the most common element/mode of `2,2, and 49` is `2`, so the result for run `002` is `2`.

_A couple important notes: `A`-`B` is guaranteed to be a positive, integer value for all entries, and there is guaranteed to be a single unique mode for each run_

Currently, what I’m doing is this:

```auto
// Some code above

map<int, map<int, int>> offset;

df.Foreach([](int A, int B, int run){
    ++map[run][A-B];
},{"A","B","run"});

// Some code that, for each map in the map, determines the key of the 
// max value (i.e. the mode), and stores this in a vector

```

So, we have a map of maps. The “outer” map maps run numbers to “interior” maps. “Interior” maps map offset (`A-B`) to frequency. I then, for each “interior” map, get the key of the largest value (i.e. the mode), and store this in a new map, which maps run numbers to mode of `A-B` for that run.

* * *

This works, however it’s quite slow (and, for my purposes, impractically so). My `RDataFrame` contains on the order of tens of millions or more entries.

I imagine there must be a better way. I’ve spent some time with the `RDataFrame` docs and, unfortunately, I can’t seem to piece together anything measurably more efficient.

Is there a better way? Please, let me know if the issue at hand isn’t clear. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [September 26, 2021, 5:58am UTC](https://root-forum.cern.ch/t/better-way-to-determine-most-common-difference-of-two-columns-of-an-rdataframe/46996/2 "2021-09-26T05:58:21Z")

</div>

I see you posted this problem before and looks like the ROOT suggestions are not good or fast enough, so probably you should google/whatever search for c++ solutions. Perhaps something like this can help  
[https://www.codeproject.com/articles/866996/fast-implementations-of-maps-with-integer-keys-in](https://www.codeproject.com/articles/866996/fast-implementations-of-maps-with-integer-keys-in)  
or you may be able to devise your own solution, which might be more efficient for your specific case.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 22, 2022, 10:43am UTC](https://root-forum.cern.ch/t/better-way-to-determine-most-common-difference-of-two-columns-of-an-rdataframe/46996/3 "2022-06-22T10:43:15Z")

</div>

Hi,

I just came across this thread that somehow I had completely missed at the time. Sorry about that!

@KAM if you are still around I would be curious to hear how you would code an algorithm that does that independently of RDataFrame – then we can think how to fit that algorithm in RDF’s API.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![KAM](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kam/32/37110_2.png) [@KAM](https://root-forum.cern.ch/u/KAM)\
**Post date:** [June 24, 2022, 9:09pm UTC](https://root-forum.cern.ch/t/better-way-to-determine-most-common-difference-of-two-columns-of-an-rdataframe/46996/4 "2022-06-24T21:09:09Z")

</div>

Sure thing-- I’ll try to find and post the code I wrote soon.
