<?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: JSL - Calculate Mean of Last 3 Rows Per Date in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/426140#M67581</link>
    <description>&lt;P&gt;Other option with a formula:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;If(Row() == Col Max(Row(), :Date),
	Mean(:Data[Index(Row() - 2, Col Max(Row(), :Date))])
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Oct 2021 16:21:46 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2021-10-13T16:21:46Z</dc:date>
    <item>
      <title>JSL - Calculate Mean of Last 3 Rows Per Date</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/426090#M67570</link>
      <description>&lt;P&gt;I have a data set where I'd like to calculate the mean of the last 3 observations for each date and have that value be in its own column and paired with the last date as shown below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

// Create an example data table
dt = New Table( "Date",
	Add Rows( 30 ),
	New Column( "Date",
		Numeric,
		"Continuous",
		Format( "m/d/y", 10 ),
		Set Values( [3713817600, 3713817600, 3713817600, 3713817600, 3713817600, 3713817600, 3713817600, 3713817600, 3713817600, 3713817600, 
		3715027200, 3715027200, 3715027200, 3715027200, 3715027200, 3715027200, 3715027200, 3715027200, 3715027200, 3715027200,
		3716236800, 3716236800, 3716236800, 3716236800, 3716236800, 3716236800, 3716236800, 3716236800, 3716236800, 3716236800] )
	),

	New Column( "Data",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [2,4,6,8,10,12,14,16,18,20,2,4,6,8,10,12,14,26,28,30,2,4,6,8,10,12,14,36,38,40] )
	),	
	New Column( "Desired Result",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [.,.,.,.,.,.,.,.,.,18,.,.,.,.,.,.,.,.,.,28,.,.,.,.,.,.,.,.,.,38] )
	),	
	
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While I can create a summary table that calculates the mean and then insert the mean back in the original data table, I'm looking for a suggestion for a more elegant and less process intensive method.&amp;nbsp; Any suggestions on how to accomplish this would be appreciated, either as a column formula, or via JSL (preferred).&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:02:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/426090#M67570</guid>
      <dc:creator>terapin</dc:creator>
      <dc:date>2023-06-09T18:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: JSL - Calculate Mean of Last 3 Rows Per Date</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/426103#M67573</link>
      <description>&lt;P&gt;Here is one way to write the formula to do this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( :Date != :Date[Row() + 1] | Row() == N Rows( Current Data Table() ),
	Mean( :Data[Row()], :Data[Row() - 1], :Data[Row() - 2] )
)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Oct 2021 15:37:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/426103#M67573</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2021-10-13T15:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: JSL - Calculate Mean of Last 3 Rows Per Date</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/426140#M67581</link>
      <description>&lt;P&gt;Other option with a formula:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;If(Row() == Col Max(Row(), :Date),
	Mean(:Data[Index(Row() - 2, Col Max(Row(), :Date))])
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Oct 2021 16:21:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/426140#M67581</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-10-13T16:21:46Z</dc:date>
    </item>
    <item>
      <title>Re: JSL - Calculate Mean of Last 3 Rows Per Date</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/427078#M67675</link>
      <description>&lt;P&gt;Thanks for the suggestion, especially one that I could easily wrap my head around.&amp;nbsp; I'll need to dig into the suggestion by jthi more since the solution isn't as intuitive to me as yours is.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 16:28:01 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/427078#M67675</guid>
      <dc:creator>terapin</dc:creator>
      <dc:date>2021-10-15T16:28:01Z</dc:date>
    </item>
    <item>
      <title>Re: JSL - Calculate Mean of Last 3 Rows Per Date</title>
      <link>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/427081#M67676</link>
      <description>&lt;P&gt;I can also open my solution a bit as Col functions can be fairly powerful. Take a look at the explanation when you have time:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Use Col Max(Row(), :Date) to get last row for each date&lt;/LI&gt;&lt;LI&gt;Check with the if-statement with Row() for last row for date&lt;/LI&gt;&lt;LI&gt;Use Mean to calculate mean of last three rows for a date. Index(Row() - 2, Col Max(Row(), :Date) creates matrix of row numbers. And then with:Data[Index] we can get values for those row numbers&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I have attached datatable in which the formula is split in parts&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1634315733838.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/36737iC63FE30513EEE9DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1634315733838.png" alt="jthi_0-1634315733838.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Oct 2021 16:36:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JSL-Calculate-Mean-of-Last-3-Rows-Per-Date/m-p/427081#M67676</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-10-15T16:36:50Z</dc:date>
    </item>
  </channel>
</rss>

