Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- JMP User Community
- :
- Discussions
- :
- Re: Scripting average of last five points for each row type

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

May 8, 2017 7:17 AM
(6189 views)

Hi,

I am having issues trying to figure out the best way to average only the last five data points for each row identifier using jsl. I tried to set a sequence and then delete the first x points of the sequence, but the issue is that not each row identifier has the same number of rows. The number of identifiers is also subject to change based on the data pull.

Ex, for the following table I want to output the mean of just the last five points from A, B and C (but the identifier list could go all the way up to Z):

1 ACCEPTED SOLUTION

Accepted Solutions

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Would something like this work?

avgs = Associative Array( dt:ID ); ids = avgs << get keys; For( i = 1, i <= N Items(ids), i++, these = As List( dt << get rows where( dt:ID == ids[i] ) ); If( N Items(these) < 5, n = N Items(these), n = 5; Remove From( these, 1, N Items(these) - n ); ); these = Matrix( these ); avgs[ids[i]] = Sum(dt:Value[these]) / n; ); Show(avgs);

4 REPLIES 4

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Would something like this work?

avgs = Associative Array( dt:ID ); ids = avgs << get keys; For( i = 1, i <= N Items(ids), i++, these = As List( dt << get rows where( dt:ID == ids[i] ) ); If( N Items(these) < 5, n = N Items(these), n = 5; Remove From( these, 1, N Items(these) - n ); ); these = Matrix( these ); avgs[ids[i]] = Sum(dt:Value[these]) / n; ); Show(avgs);

Highlighted
##

Thank you! I was able to adapt this to what I needed. The next question is though, how can I spit these average values out to a summary table?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Scripting average of last five points for each row type

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Scripting average of last five points for each row type

You can just create a new table using the keys and values of the associative array:

dtSummary = New Table( "Summary", New Column( "ID", "Character", Set Values( avgs << Get Keys ) ), New Column( "Average", "Numeric", Set Values( avgs << Get Values ) ) );

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Scripting average of last five points for each row type

Works like a charm, thanks!

Article Labels

There are no labels assigned to this post.