Average calories of "moderate" food - google-sheets

I keep a spreadsheet with caloric information about food. Each row represents a product labelled as {negative, small, moderate} and the number of calories.
I included a shared copy of the spreadsheet.
Table
Shared table
I would like to calculate the average of the numbers that are in the same row as the keyword 'moderate'. For instance, I would like to obtain something like (890+914+731+1159+789)/5=897. I have tried =AVERAGE(B3,B7:B10) and it works but it needs to be modified when I add another product.
The expected output is in red. I want to obtain such output using formulas.
Thanks in advance

Maybe you can also consider the use of AVERAGEIF() as it seems the function is built for situations like this:
=averageif(A2:A; "moderate"; B2:B)

When you insert new row, formula will be modified automatically. Also, your formula is wrong, missing semicolon:
=AVERAGE(B3; B7:B10)
^

You can Use a IF Statement. Then you automatically do the average of the numbers IF the respective value is moderate. This is better, because it will work, if you have 5 or 5000 Moderates
Just use: =AVERAGE(FILTER(B2:B10;(A2:A10="moderate")))

You could also use SUMIF with COUNTIF, it's a more basic formula, not as powerful as FILTER, but just as funcitonal. Also to use unlimited ROWs, you can remove the number on the second part of the RANGE, this for any formula, in this case your formula would be as such:
=SUMIF(A2:A;"moderate";B2:B)/COUNTIF(A2:A;"moderate")

Related

How do I create a formula that recommends a matchup of 2 names?

Iโ€™m trying to make a formula that accomplishes this:
From a list of numbers, find the pairs of numbers with the lowest difference and second lowest difference, and write their differences elsewhere(I donโ€™t know how to do this)
Use those numbers to find and use the value(a name)of the cell to their left(I donโ€™t know how to do this with duplicate numbers)
How would I go about this?
The final product should include all 4 numbers, names, and both differences.
In the picture below, the pink shaded section is the goal.
What I did is to first SORT the values:
=SORT(A2:B,2,1)
Then, wrapped it in LAMBDA (in order not to repeat those formula again and again), and started "scanning" those values to find the differences. Since it's already sorted, the "smallest" will be found in this comparison.
For this, I used MAP. Used SEQUENCE, COUNTA and INDEX in order to find the amount of values in the first column; and SEQUENCE will help me do the "navigation". With the variable resultant "p" I searched each row with the next one (p+1). Resulting in something like this:
=LAMBDA(sorted,MAP(SEQUENCE(COUNTA(INDEX(sorted,,1))-1),LAMBDA(p,{INDEX(sorted,p,1),INDEX(sorted,p+1,1),INDEX(sorted,p+1,2)-INDEX(sorted,p,2)})))(SORT(A2:B,2,1))
With that, you're already one step away. Just with SORTN you can find the four smallest differences:
=LAMBDA(diff,SORTN(diff,4,1,3,1))
(LAMBDA(sorted,MAP(SEQUENCE(COUNTA(INDEX(sorted,,1))-1),LAMBDA(p,{INDEX(sorted,p,1),INDEX(sorted,p+1,1),INDEX(sorted,p+1,2)-INDEX(sorted,p,2)})))
(SORT(A2:B,2,1)))
I hope it's already useful! You can split those values in two columns, obviously; just using INDEX to access the desired row of this formula; or just locate the formula somewhere and refer to those cells. Let me know if it's useful!
you can try:
=LAMBDA(z,SORTN(MAP(INDEX(z,,1),INDEX(z,,2),LAMBDA(a,b,{a,b,ABS(XLOOKUP(a,A:A,B:B,)-XLOOKUP(b,A:A,B:B,))})),4,0,3,1))(INDEX(SPLIT(QUERY(UNIQUE(MAP(LAMBDA(z,FLATTEN(z& "๐Ÿ " &TRANSPOSE(z)))(FILTER(A2:A, A2:A<>"")),LAMBDA(y,JOIN("๐Ÿ ",SORT(UNIQUE(TRANSPOSE(SPLIT(y,"๐Ÿ ")))))))),"Select Col1 Where Col1 CONTAINS '๐Ÿ '"),"๐Ÿ ")))

How can I multiply together every nth cell in a row to get the product?

I want to calculate the product of the investment returns (compound interest), which in this instance would give a return of 162%:
Additional stock return data will be added in future, so I need a formula that compounds every nth (2nd) value. I am able to sum the cells using:
=SUMIF(ARRAYFORMULA(MOD((COLUMN(A2:H2)-COLUMN(A2)+1),2)),0,A2:H2)
However I cannot find an equivalent 'PRODUCTIF' function and have not been able to make a formula that works. Writing the formula out long hand would be:
=B2*D2*F2*H2
Product formula should be like Sum formula but putting 1 for odd columns:
=ArrayFormula(PRODUCT(IF(MOD(COLUMN(A2:H2)-COLUMN(A2)+1,2),1,A2:H2)))
I guess i'm a little confused. doesn't PRODUCT() just ignore text?
Doesn't =PRODUCT(2:2) work?
You can also use the following formula:
=PRODUCT(TRANSPOSE(2:2))
and format the result as Percent from the menu
Functions used:
TRANSPOSE
PRODUCT
EDIT
As correctly noted by Mat, an even simpler formula can also give the correct results.
=PRODUCT(2:2)

How to calculate an average in Google Sheets excluding outliers which are manually specified in a separate column?

I have a Google Sheet containing a column with values, Price/sqft, which I would like to average, but only including values for which an adjacent column, Outlier? does not contain the word yes.
I took a stab at it following the AVERAGEIF documentation, but I'm getting a 'divide-by-zero' error:
Another way to do it would be to create a separate column which contains the conditional value and compute a regular AVERAGE on that. Is this the way to go? Or is there a way to do this in one go using AVERAGEIF?
You should use the formula =AVERAGEIFS(Q5:Q13,S5:S13,"<>yes")
Here, the syntax is like AVERAGEIFS(average_range, criteria_range1, criterion1)
I hope this will be helpful.
If you do use Averageif, you need to put the criterion range first and the average range last
=averageif(B2:B10,"<>Yes",A2:A10)

Stacking multiple columns on to one?

I am using Google SpreadSheet, and I'm trying to have multiple sheets containg a list of words. On the final sheet, I would like to create a summative list, which is a combination of all the values in the column. I got it sort working using =CONCATENATE() , but it turned it into a string. Any way to keep it as a column list?
Here is an example as columns:
Sheet1
apple
orange
banana
Sheet2
pineapple
strawberry
peach
FinalSheet
apple
orange
banana
pineapple
strawberry
peach
Updated Answer
I was right there is a much better solution. It's been posted below but I'm copying it here so it's in the top answer:
=unique({A:A;B:B})
Caveat: This will include one blank cell in certain scenarios (such as if there's one at the end of the first list).
If you're not concerned with ordering and a tailing blank cell a simple sort() will clean things up:
=sort(unique({A:A;B:B}))
Otherwise a filter() can remove the blanks like so:
=filter(unique({A:A;B:B}),NOT(ISBLANK(unique({A:A;B:B}))))
The following is the old deprecated answer
I'm confident that this is "The Wrong Way To Do It", as this seems such an absurdly simple and common task that I feel I must be missing something as it should not require such an overwrought solution.
But this works:
=UNIQUE(TRANSPOSE(SPLIT(JOIN(";",A:A,B:B),";")))
If your data contains any ';' characters you'll naturally need to change the delimiter.
The basic way, is just to do it as arrays like so
={A1:A10;B1:B10...etc}
The problem with this method, as I found out is that its very time consuming if you have lots of columns.
I've done some searching around and have come across this article:
Joining Multiple Columns Into One Sorted Column in Google Spreadsheets
The core formula is
=transpose(split(arrayformula(concatenate(if(len(A:Z)>0,A:Z&";",""))),";"))
Obviously you'd replace the A:Z to whatever range you want to use.
And if you want to do some sorting or removing duplicates, you'd simply wrap the the above formula in a SORT() and/or UNIQUE() method, like so..
=sort(unique(transpose(split(arrayformula(concatenate(if(len(A:Z)>0,A:Z&";",""))),";"))))
Hope this helps.
Happy coding everyone :)
You can use this:
=unique({A1:A;B1:B})
Works perfect here!
The unique() function gets rid of blank spaces, but wasn't helpful for me because some of my rows repeat. Instead I first filter the columns by len() to remove blank cells. Then I combine the columns together in the same way.
={filter(A:A, len(A:A)); filter(B:B, len(B:B))}
Much more simple:
={sheetone!A2:A;sheettwo!A2:A}
Use flatten, e.g. flatten(A1:B2). More details in this article.
If the 2d range is not in one piece, one can be created first with the ampersand or similar techniques. Afterwards flatten can be called on the resulting 2d range. The below example is a bit overkill but it is nice when working with dynamic 2d ranges, where the basic solution can't be easily used.
flatten(ARRAYFORMULA(SPLIT(ARRAYFORMULA(A1:A2&";"&C3:C4), ";")))
The article shows also how to easily unflatten a range using the, as well undocumented, skipping clause in a query.
=TRANSPOSE(SPLIT(TEXTJOIN("#",TRUE,TRANSPOSE(A:C),TRANSPOSE(D1:D5)),"#",FALSE,FALSE))
use a preferred delimiter absent in the data (instead of #) if needed
the first 1 (TRUE) parameter means IGNORE EMPTY, which is very important in this case..
the A:C and D1:D5 are the ranges to combine
all values remain there - not using UNIQUE
Try using your CONCATENATE argument with
=ArrayFormula(EXPAND(...))

Google Spreadsheet range names

In Google Docs Spreadsheets, one can use Range Names to put labels on ranges of cells to make formulas more legible. In most formulas, one can use the range C:C to denote the entire C column, and C2:C to denote the entire C column after and including C2.
Is there a way to create range names of the same nature? When I try C:C or C2:C or Sheet!C:C or 'Sheet'!C:C I always get the error "The range you specified is not in a valid range format." I would like the range name to expand as my form adds rows to my spreadsheet. Thanks.
I just discovered the if you use the '-' operator, it starts from the bottom row. So,
=INDIRECT("-D:D12")
starts from the last row and works it's way up to D12!
I had a similar problem. Although I do not know how to do exactly what you are asking, you can do essentially the same thing by referencing cells that are not yet created.
For example:
Column C currently has 100 cells (100 rows in the sheet)
Instead of referencing it with C:C, use C1:C999
If you make the row reference high enough, then you can account for future rows that you will create. Hope it helps.
I don't think so... even if you select a column manually while in the Range Name selector, it complains. That would be a nice feature and it would make sense since they support column ranges for formulas already.
I believe this does work now. I have a range name of "Sheet1!A10:AW10" with no problems.
If you try to do a whole column, I think it will just take all the available cells in the column at that time. i.e. if you make more cells later, you need to manually add to the range name.
I had the same problem with ranges such as A3:A which normally work in other places such as ARRAYFORMULA(), but the workaround is to not specify the starting row, such as A:A. In cases when this would be a problem, you can proxy the data through another column using something like ARRAYFORUMULA(A25:A) as the formula.
Update: Apparently I haven't read the question properly. I see that the OP had tried leaving out the row number, so perhaps it wasn't working at that time, but it does now. The notations still don't work.
Update2: I didn't notice that google spreadsheet replaces ranges like A:A to A1:A50, so new rows added later on do not still get included. That I think is what #Dean is trying to say in his answer.
I think it's a helpful tool to use Insert -> Define new range to make a wizard appear and make the syntax correct. Hehe
My response in other topic

Resources