Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the sum of the absolute value of the difference between two columns?

I am trying to find the Spearman's Footrule Distance between two columns in google sheets in only one cell. The Spearman's Footrule Distance basically finds the distance between two vectors by summing the absolute value of the differences between the elements at each index of the vector. For example, the distance between (-1, 0, 2) and (1, -1, 2) is (|-1-1| + |0--1| + |2-2|) = (|-2|+|1|+|0|) = 2+1+0 = 3. I have a formula that finds the sum of the difference between the two columns, but I can't figure out how to make it sum the absolute value of the difference.

So far, this is what I have: =SUMPRODUCT(B1:B3>A1:A3,B1:B3-A1:A3). This returns exactly what it is supposed to return, but I want it to be the absolute value of the difference. How can I achieve this?

Side Note: I have to find this in only one cell because my co-editors do not want any extra columns (hidden or not) in this particular spreadsheet.

spreadsheet (sample data in the first worksheet): https://docs.google.com/spreadsheets/d/12CXk-vzJxYaEhD1QsAXx25JRDDubnqV9zAvG2sc1ykw/edit#gid=64105883

like image 312
Jodast Avatar asked Oct 15 '25 09:10

Jodast


2 Answers

You can do

=ARRAYFORMULA(SUM(ABS(A1:A3-B1:B3)))

ARRAYFORMULA is nice for applying an operation to multiple ranges/arrays and getting back the result as an array so that you can do things to that (like sum).

Enables the display of values returned from an array formula into multiple rows and/or columns and the use of non-array functions with arrays.

See docs here.

like image 186
Amit Kumar Gupta Avatar answered Oct 17 '25 02:10

Amit Kumar Gupta


this would also work:

=ARRAYFORMULA(SUM(SUBSTITUTE(A1:A3-B1:B3, "-", )*1))

0

like image 38
player0 Avatar answered Oct 17 '25 04:10

player0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!