Need some help in understanding this Google Sheets arrayformula function - google-sheets

The function is:
=arrayformula( (NORMINV(RANDarray(1,4000),$K3,$L3) * AVERAGE($G3,1)) * $J3 )
My understanding is that this creates a list of 4000 random values and for each random value, it passes that value to the NORMINV function along with the fixed values in the cells K3 and L3 as parameters. The return value of this function is then multiplied with the average of the value in the cell G3 and the number 1 and this in turn is multiplied by the value in the cell J3. Is my understanding of this correct?

yes, that is correct. RANDARRAY will generate a row with 4000 columns with random numbers between 0 and 1. this array is then used in NORMINV and multiplied by AVERAGE between G3 and 1 and then multiplied again with J3

Related

Filter column that other columns don't have only positive values and don't have only negative values

Currently to make this filter I use this formula:
=FILTER(AC2:AC,
NOT((S2:S>0)*(T2:T>0)*(U2:U>0)*(V2:V>0)*(W2:W>0))*
NOT((S2:S<0)*(T2:T<0)*(U2:U<0)*(V2:V<0)*(W2:W<0))
)
I would like to know if there is a correct and more direct method for this same result.
Your title is a bit confusing, but from your formula my interpretation is that you are filtering out rows from column AC where the corresponding row in columns S:W are all either positive or negative. You could try:
=filter(AC2:AC,byrow(S2:W,lambda(row,abs(sum(sign(row)))<>5)))
SIGN returns 1 for +ve numbers and -1 for -ves. We can get the ABS value of the SUM of SIGNs per row with BYROW, so it's simply a case of FILTERing by those rows not equal to 5 (only observed for all +ves or all -ves in a row).

Summing over values in Google Sheets after applying a formula

I have a column of strings of the form
n|m
where n<m are natural numbers and I want to calculate the sum of this column where each cell has value m-n (m minus n). I can calculate the respective values into a separate cell via:
=INDEX(SPLIT(A1,"|"), 0,2)-INDEX(SPLIT(A1,"|"), 0,1)
and sum over those but I would like to do it in one step just as one formula below the specified column in one cell. Is that possible?
Thanks
Try in B1
=ARRAYFORMULA(SUM(IFERROR(INDEX(SPLIT(A1:A,"|"), 0,2)-INDEX(SPLIT(A1:A,"|"), 0,1))))
if you want to get the sum below the last value, i.e. row#8
=ARRAYFORMULA(SUM(IFERROR(INDEX(SPLIT(A1:A8,"|"), 0,2)-INDEX(SPLIT(A1:A8,"|"), 0,1))))

Formula breaks when placed inside arrayformula

Background: In the formulas below, I am trying to find any number that has the sequential pattern of +1. Example: 1011 (after 10, comes 11) =(FirstTwo+1=11)=Last 2.
Current Formulas:
A1 =ARRAYFORMULA(ROW(1000:1011)) //List numbers from 1000 to 1011.
B1 =ARRAYFORMULA(LEFT(A1:A,2)) //Only retrieve the first 2 digits.
C1 =ARRAYFORMULA(RIGHT(A1:A,2)) //Only retrieve the last 2 digits.
D1 =ARRAYFORMULA(if((B1:B12+1=C1:C12),TRUE,FALSE)) // If first 2 digits +1 = last 2 digits, TRUE.
Expected Outcome: D12 = TRUE
Actual Outcome: D12 = FALSE
My only found solution (not a good one)
Delete formula in C1.
Manually type each number in column C (resulting in C12 containing "11")
And now the array formula works for D1, and correctly applied TRUE in D12.
Why cant I achieve the same results using the formula in C1?
The problem is that the output data type is text and not an integer. You can test it by using the function type(). Reference document link.
You can fix it by multiplying the outputs by 1. Eg.:
B1: =ARRAYFORMULA(LEFT(A1:A,2)*1)
C1: =ARRAYFORMULA(RIGHT(A1:A,2)*1)

Compute subranks in spreadsheet column in combination with ArrayFormula (Google Sheets)

I'm trying to find the inverse rank within categories using an ArrayFormula. Let's suppose a sheet containing
A B C
---------- -----
1 0.14 2
1 0.26 3
1 0.12 1
2 0.62 2
2 0.43 1
2 0.99 3
Columns A:B are input data, with an unknown number of useful rows filled-in manually. A is the classifier categories, B is the actual measurements.
Column C is the inverse ranking of B values, grouped by A. This can be computed for a single cell, and copied to the rest, with e.g.:
=1+COUNTIFS($B$2:$B,"<" & $B2, $A$2:$A, "=" & $A2)
However, if I try to use ArrayFormula:
=ARRAYFORMULA(1+COUNTIFS($B$2:$B,"<" & $B2:$B, $A$2:$A, "=" & $A2:$A))
It only computes one row, instead of filling all the data range.
A solution using COUNT(FILTER(...)) instead of COUNTIFS fails likewise.
I want to avoid copy/pasting the formula since the rows may grow in the future and forgetting to copy again could cause obscure miscalculations. Hence I would be glad for help with a solution using ArrayFormula.
Thanks.
I don't see a solution with array formulas available in Sheets. Here is an array solution with a custom function, =inverserank(A:B). The function, given below, should be entered in Script Editor (Tools > Script Editor). See Custom Functions in Google Sheets.
function inverserank(arr) {
arr = arr.filter(function(r) {
return r[0] != "";
});
return arr.map(function(r1) {
return arr.reduce(function(rank, r2) {
return rank += (r2[0] == r1[0] && r2[1] < r1[1]);
}, 1);
});
}
Explanation: the double array of values in A:B is
filtered, to get rid of empty rows (where A entry is blank)
mapped, by the function that takes every row r1 and then
reduces the array, counting each row (r2) only if it has the same category and smaller value than r1. It returns the count plus 1, so the smallest element gets rank 1.
No tie-breaking is implemented: for example, if there are two smallest elements, they both get rank 1, and there is no rank 2; the next smallest element gets rank 3.
Well this does give an answer, but I had to go through a fairly complicated manoeuvre to find it:
=ArrayFormula(iferror(VLOOKUP(row(A2:A),{sort({row(A2:A),A2:B},2,1,3,1),row(A2:A)},4,false)-rank(A2:A,A2:A,true),""))
So
Sort cols A and B with their row numbers.
Use a lookup to find where those sorted row numbers now are: their position gives the rank of that row in the original data plus 1 (3,4,2,6,5,7).
Return the new row number.
Subtract the rank obtained just by ranking on column A (1,1,1,4,4,4) to get the rank within each group.
In the particular case where the classifiers (col A) are whole numbers and the measurements (col B) are fractions, you could just add the two columns and use rank:
=ArrayFormula(iferror(rank(A2:A+B2:B,if(A2:A<>"",A2:A+B2:B),true)-rank(A2:A,A2:A,true)+1,""))
My version of an array formula, it works when column A contains text:
=ARRAYFORMULA(RANK(ARRAY_CONSTRAIN(VLOOKUP(A1:A,{UNIQUE(FILTER(A1:A,A1:A<>"")),ROW(INDIRECT("a1:a"&COUNTUNIQUE(A1:A)))},2,)*1000+B1:B,COUNTA(A1:A),1),ARRAY_CONSTRAIN(VLOOKUP(A1:A,{UNIQUE(FILTER(A1:A,A1:A<>"")),ROW(INDIRECT("a1:a"&COUNTUNIQUE(A1:A)))},2,)*1000+B1:B,COUNTA(A1:A),1),1) - COUNTIF(A1:A,"<"&OFFSET(A1,,,COUNTA(A1:A))))

How to sum the maximum value of each column in Google Spreadsheets?

I have a Google Spreadsheet of numbers. How do I take the maximum value from each column, and summarize them using only one formula? (No temp cells, no scripts.)
1 2 1
0 1 3
0 2 0
For the table above the result should be 6 (1+2+3, the maximum value of each column). But I'd like a solution that works for much larger tables, too.
As a more general question, I'd like to find out how I could fold 2D ranges into 1D arrays using an arbitrary operator (like MAX and SUM in this case).
Assuming your data in range A2:D, to get the maximum of every row (array output) try
=query(transpose(query(if(row(A2:D)>=transpose(row(A2:D)),transpose( A2:D)),"select max(Col1),max(Col2),max(Col3),max(Col4) ",0)),"Select Col2", 0)
If you need to process a lot of columns, this may be better
=ArrayFormula(QUERY(TRANSPOSE(QUERY(TRANSPOSE( A2:D) , "Select "&"MAX(Col"&JOIN( ", MAX(Col",ROW(INDIRECT( "YY1:YY"&ROWS(A2:A)))&")"))), "Select Col2", 0))
To sum, just wrap SUM() around the above formulas.
MAX by columns in A1:C3
=INDEX(QUERY({A1:C3},"Select "&"MAX(Col"&JOIN(", MAX(Col",SEQUENCE(COLUMNS(A1:C3))&")"),0),2)
MAX by rows in A1:C3
=TRANSPOSE(INDEX(QUERY(TRANSPOSE(A1:C3),"Select "&"MAX(Col"&JOIN(", MAX(Col",SEQUENCE(ROWS(A1:C3))&")"),0),2))
Substitute MAX with MIN to get the minimums.

Resources