<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I use a formula to calculate mean for selected rows in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/700197#M88495</link>
    <description>&lt;P&gt;The formula to do the calculations is actually very simple.&amp;nbsp; It is your specification to have the results only displayed on specific rows that makes the calculations a bit messy.&lt;/P&gt;
&lt;P&gt;When a formula is applied to a given column, JMP simply takes the formula and applies it to row 1, then row 2 and so on to the last row in the table.&amp;nbsp; Therefore, the below formula will give you the answers you want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Sample Number )
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically the formula calculates the mean for the column "Amount of Cyanidin (ng/kg)" and write it out to the row for the column.&amp;nbsp; Because there is also a By column specified, "Sample Number" it calculates the mean for each level found for the "Sample Number" and then writes it out to the row for the new column.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1700251759628.png" style="width: 843px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58890i2A6BF28A7468B946/image-dimensions/843x531?v=v2" width="843" height="531" role="button" title="txnelson_0-1700251759628.png" alt="txnelson_0-1700251759628.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;But your requirement was to only display the mean value on the last row for each of the "Sample Numbers".&amp;nbsp; To identify which rows those are, there needs to be something that programmatically can be evaluated.&amp;nbsp; In this case, I chose to check to see if the value of "Sample Number" changes in the row after the current row being processed.&amp;nbsp; So that changes the formula to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( :Sample Number != :Sample Number[Row() + 1] ,
	Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Sample Number )
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The If() clause is interpreted as:&amp;nbsp; if the current value of "Sample Number" is not equal to the value of "Sample Number" in the row 1 row beyond the current row( Row() + 1 ), then do the calculation.&amp;nbsp; If this comparison is not true, then it does nothing.&lt;/P&gt;
&lt;P&gt;So this works great except for the last row in the data table.&amp;nbsp; When JMP attempts to look at a row 1 row beyond the last row in the data table, it has no row to compare with, so it can not make a decision.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_2-1700253886862.png" style="width: 794px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58892iBA716102862C8EED/image-dimensions/794x110?v=v2" width="794" height="110" role="button" title="txnelson_2-1700253886862.png" alt="txnelson_2-1700253886862.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus, the formula needs to take into account this issue too.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( :Sample Number != :Sample Number[Row() + 1] | Row() == N Rows( Current Data Table() ),
	Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Sample Number )
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here the additional clause is checking to see if the current row ,Row() , is equal to the number of rows in the data table, N Rows( Current data Table().&amp;nbsp; The second clause is separated by "|" which indicates that if either the first clause "OR" the second clause is true then process the calculation.&lt;/P&gt;
&lt;P&gt;The second formula column follows the same logic with just the By column being changed to "Developmental Stage".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Concerning what needs to change to be able to use this on your full data table.&amp;nbsp; I do not know what you are referring to when you say, "&lt;SPAN&gt;cultivar".&amp;nbsp; &amp;nbsp;The requirements for the formulas&amp;nbsp;to work properly are for the data to be sorted in order of the "Developmental Stage" and "Sample Number" and that "Amount of Cyanidin (ng/kg)" is the name of the column that the means are calculated on.&amp;nbsp; If any of those columns have a change in name, then the new names need to be swapped out in the formulae.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Nov 2023 20:45:15 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2023-11-17T20:45:15Z</dc:date>
    <item>
      <title>How do I use a formula to calculate mean for selected rows</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/699546#M88442</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To preface, I am basically a new user to JMP so I don't know how to do any scripting.&amp;nbsp; Just basic formulas, etc.&amp;nbsp; Here is what I need to do and can't figure out how.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to first calculate an average of the "Amount of Cyandin" for each of the same "Sample Number" (goal to have a column that calculates what I've placed in the "Average of three injections" column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Similarly I'd like a column that gives the average of those three averages for each "Development Stage" which I've placed the goal in the column labeled "Average of developmental stages"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's not a moving average... I thought maybe a conditional statement in a formula would work? Or perhaps a formula that uses some kind of third column like the "ID Code" column as an identifier to tell it which rows to average?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2023 00:58:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/699546#M88442</guid>
      <dc:creator>EmilyT</dc:creator>
      <dc:date>2023-11-17T00:58:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a formula to calculate mean for selected rows</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/699567#M88444</link>
      <description>&lt;P&gt;The 2 formulas you need are&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( :Sample Number != :Sample Number[Row() + 1] | Row() == N Rows( Current Data Table() ),
	Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Sample Number )
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( :Developmental Stage != :Developmental Stage[Row() + 1] | Row() == N Rows( Current Data Table() ),
	Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Developmental Stage )
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1700188615699.png" style="width: 846px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58824i025EDE010C82A6EF/image-dimensions/846x476?v=v2" width="846" height="476" role="button" title="txnelson_0-1700188615699.png" alt="txnelson_0-1700188615699.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2023 02:37:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/699567#M88444</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-11-17T02:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a formula to calculate mean for selected rows</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/700030#M88484</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the reply!&amp;nbsp; I tried it and it works.&amp;nbsp; I just don't quite understand how it works....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far I think I get that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the sample number doesn't equal the sample number of the next row&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OR&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the current row number is equal to the total rows in the current data table? (this part I'm confused on.&amp;nbsp; Plus when I enter this, I get a warning about using the "Current Data Table" part.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;THEN&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Calculate the mean&amp;nbsp; of the "Amount of Cyanidin (ng/kg) when the Sample Numbers are the same?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could the workings of the formula be explained a bit to this beginner?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Followup question - for my full data table I will have obviously more data with rows that have a different cultivar name but then same sample numbers and injection numbers as shown in my example.&amp;nbsp; Will these formulas still work?&amp;nbsp; Will I need to account for the Cultivar name to separate them out?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2023 17:56:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/700030#M88484</guid>
      <dc:creator>EmilyT</dc:creator>
      <dc:date>2023-11-17T17:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a formula to calculate mean for selected rows</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/700197#M88495</link>
      <description>&lt;P&gt;The formula to do the calculations is actually very simple.&amp;nbsp; It is your specification to have the results only displayed on specific rows that makes the calculations a bit messy.&lt;/P&gt;
&lt;P&gt;When a formula is applied to a given column, JMP simply takes the formula and applies it to row 1, then row 2 and so on to the last row in the table.&amp;nbsp; Therefore, the below formula will give you the answers you want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Sample Number )
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically the formula calculates the mean for the column "Amount of Cyanidin (ng/kg)" and write it out to the row for the column.&amp;nbsp; Because there is also a By column specified, "Sample Number" it calculates the mean for each level found for the "Sample Number" and then writes it out to the row for the new column.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1700251759628.png" style="width: 843px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58890i2A6BF28A7468B946/image-dimensions/843x531?v=v2" width="843" height="531" role="button" title="txnelson_0-1700251759628.png" alt="txnelson_0-1700251759628.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;But your requirement was to only display the mean value on the last row for each of the "Sample Numbers".&amp;nbsp; To identify which rows those are, there needs to be something that programmatically can be evaluated.&amp;nbsp; In this case, I chose to check to see if the value of "Sample Number" changes in the row after the current row being processed.&amp;nbsp; So that changes the formula to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( :Sample Number != :Sample Number[Row() + 1] ,
	Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Sample Number )
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The If() clause is interpreted as:&amp;nbsp; if the current value of "Sample Number" is not equal to the value of "Sample Number" in the row 1 row beyond the current row( Row() + 1 ), then do the calculation.&amp;nbsp; If this comparison is not true, then it does nothing.&lt;/P&gt;
&lt;P&gt;So this works great except for the last row in the data table.&amp;nbsp; When JMP attempts to look at a row 1 row beyond the last row in the data table, it has no row to compare with, so it can not make a decision.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_2-1700253886862.png" style="width: 794px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58892iBA716102862C8EED/image-dimensions/794x110?v=v2" width="794" height="110" role="button" title="txnelson_2-1700253886862.png" alt="txnelson_2-1700253886862.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus, the formula needs to take into account this issue too.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( :Sample Number != :Sample Number[Row() + 1] | Row() == N Rows( Current Data Table() ),
	Col Mean( :"Amount of Cyanidin (ng/kg)"n, :Sample Number )
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here the additional clause is checking to see if the current row ,Row() , is equal to the number of rows in the data table, N Rows( Current data Table().&amp;nbsp; The second clause is separated by "|" which indicates that if either the first clause "OR" the second clause is true then process the calculation.&lt;/P&gt;
&lt;P&gt;The second formula column follows the same logic with just the By column being changed to "Developmental Stage".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Concerning what needs to change to be able to use this on your full data table.&amp;nbsp; I do not know what you are referring to when you say, "&lt;SPAN&gt;cultivar".&amp;nbsp; &amp;nbsp;The requirements for the formulas&amp;nbsp;to work properly are for the data to be sorted in order of the "Developmental Stage" and "Sample Number" and that "Amount of Cyanidin (ng/kg)" is the name of the column that the means are calculated on.&amp;nbsp; If any of those columns have a change in name, then the new names need to be swapped out in the formulae.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2023 20:45:15 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/700197#M88495</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-11-17T20:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a formula to calculate mean for selected rows</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/700366#M88513</link>
      <description>&lt;P&gt;Other option is to use If with Col Min and Col Max functions, but be careful if you were to use this as this might break in any of the future JMP versions due to usage of Row() with Col Max/Col Min (&lt;LI-MESSAGE title="Make using formula expressions in Set Each Value and using expressions as first argument in Col statistical formulas supported behaviour" uid="666966" url="https://community.jmp.com/t5/JMP-Wish-List/Make-using-formula-expressions-in-Set-Each-Value-and-using/m-p/666966#U666966" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-idea-thread lia-fa-icon lia-fa-idea lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt; ) &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ColAvg1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(Row() == Col Max(Row(), :Sample Number),
	Col Mean(:"Amount of Cyanidin (ng/kg)"n, :Sample Number)
)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;ColAvg2&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(Row() == Col Max(Row(), :Developmental Stage),
	Col Mean(:CoAvg1, :Developmental Stage)
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1700376384183.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/58900i1835CA5E1F58027C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1700376384183.png" alt="jthi_0-1700376384183.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Nov 2023 07:07:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/700366#M88513</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-11-19T07:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a formula to calculate mean for selected rows</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/702642#M88698</link>
      <description>&lt;P&gt;Thank you very much.&amp;nbsp; I think I've figured it out now thanks to your explanations!&lt;/P&gt;</description>
      <pubDate>Fri, 24 Nov 2023 23:34:15 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/702642#M88698</guid>
      <dc:creator>EmilyT</dc:creator>
      <dc:date>2023-11-24T23:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a formula to calculate mean for selected rows</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/937597#M109282</link>
      <description>&lt;P&gt;I know this is an old thread but I was searching for the same thing and I found an old script of mine that does this very easily. Not sure where I got it from originally but I am thrilled it works. This example below is for a range of rows.&lt;/P&gt;
&lt;DIV&gt;&lt;SPAN&gt; row_range_lower = rownum1;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; row_range_upper = rownum2;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dtx&amp;lt;&amp;lt;clear select;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; rowrangefull=dtx&amp;lt;&amp;lt;select rows(row_range_lower::row_range_upper);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; freq_avg = Col mean(If( Selected(),name of column), . );&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 26 Mar 2026 20:59:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-use-a-formula-to-calculate-mean-for-selected-rows/m-p/937597#M109282</guid>
      <dc:creator>jakeb3482</dc:creator>
      <dc:date>2026-03-26T20:59:39Z</dc:date>
    </item>
  </channel>
</rss>

