<?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: Last 30 pieces for each row and delete the other data in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412532#M66209</link>
    <description>&lt;P&gt;&amp;nbsp;FWIW, here's yet another version using matrices:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

// Make a data table 'dt', to play with.
nr = 100;		// Number of rows
nc = 30;		// Number of columns
p = 0.7;		// Proportion of missing values
vals = J(nr*nc, 1, RandomInteger(0, 99));
makeMissing = RandomIndex(nr*nc, Floor(nr*nc*p));
vals[makeMissing] = .;
dt = AsTable(Shape(vals, nr, nc));
dt &amp;lt;&amp;lt; setName("Random Integers with some missing");

// Start here with the current table . . .
lastN = 30;														// Number of non-missing values to retain
dt = CurrentDataTable();
vals = dt &amp;lt;&amp;lt; getAsMatrix;
// Loop over columns . . .
newVals = [];
for(c=1, c&amp;lt;=NCol(vals), c++,
	NMV = vals[LocNonMissing(vals[0, c]), c];					// Non-mising values in this column
	if(NRow(NMV) &amp;gt;= lastN,										// Do we have enough non-missing values?
		lastNMV = NMV[(NRow(NMV) - lastN + 1)::NRow(NMV)];		// If so, get the 'lastN' of these  . . .
		newVals = newVals||lastNMV;								// . . . and add them into a new column
		);
);
dt2 = AsTable(newVals);
dt2 &amp;lt;&amp;lt; setName("Last "||Char(lastN)||" non-missing Random Integers");&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 25 Aug 2021 14:08:00 GMT</pubDate>
    <dc:creator>ian_jmp</dc:creator>
    <dc:date>2021-08-25T14:08:00Z</dc:date>
    <item>
      <title>Last 30 pieces for each row and delete the other data</title>
      <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412025#M66157</link>
      <description>&lt;P&gt;Hello Everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data table that has multiple columns and each column has a different amount of rows with data in it. I am trying to find a way to get the last 30 rows of the data in each column. For example column 1 has 50 rows and column 2 has 75 rows and I want each row to have the last 30 rows of info. Is there a script that can do that?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 23:35:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412025#M66157</guid>
      <dc:creator>rmthomas</dc:creator>
      <dc:date>2023-06-10T23:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: Last 30 pieces for each row and delete the other data</title>
      <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412046#M66158</link>
      <description>&lt;P&gt;Deleting all but the last 30 rows in a data table is easy.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();
dt &amp;lt;&amp;lt; select where( Row() &amp;gt; N Rows( dt ) - 30 );
dt &amp;lt;&amp;lt; delete rows;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are asking to find the last 30 non missing data points for each column, and then save that as your n columns by 30 rows of data, that is a little more difficult and I will question the validity of doing that.&amp;nbsp; The result of that would be removing pretty much any relationship between data points between the different columns.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();

// Copy the data table retaining no rows;
dtCopy = dt &amp;lt;&amp;lt; subset( selected rows( 0 ), selected columns( 0 ) );
dtCopy &amp;lt;&amp;lt; select where( Row() &amp;gt;= 0 );
dtCopy &amp;lt;&amp;lt; delete rows;
For( i = 1, i &amp;lt;= N Cols( dtCopy ), i++,
	theList = {};
	For( k = N Rows( dt ), k &amp;gt;= 1, k--,
		If( Is Missing( Column( dt, i )[k] ) == 0,
			Insert Into( theList, Column( dt, i )[k] );
			If( N Items( theList ) &amp;gt;= 30,
				Break()
			);
		)
	);
	Column( dtCopy, i ) &amp;lt;&amp;lt; set values( Reverse( theList ) );
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Aug 2021 20:13:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412046#M66158</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2021-08-23T20:13:59Z</dc:date>
    </item>
    <item>
      <title>Re: Last 30 pieces for each row and delete the other data</title>
      <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412161#M66177</link>
      <description>&lt;P&gt;Here is another version to accomplish the same result as the second solution from&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;For( col = 1, col &amp;lt;= N Col( dt ), col++,
	values = Column( dt, col ) &amp;lt;&amp;lt; Get Values;
	end = Contains( values, . ) - 1;
	If( end == -1, end = N Row( dt ) );
	view = end - 29 :: end;
	Column( dt, col ) &amp;lt;&amp;lt; Set Values( values[view] );
);
dt &amp;lt;&amp;lt; Select Where( Row() &amp;gt; 30 ) &amp;lt;&amp;lt; Delete Rows;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Aug 2021 13:10:24 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412161#M66177</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2021-08-24T13:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Last 30 pieces for each row and delete the other data</title>
      <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412511#M66207</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/5358"&gt;@Mark_Bailey&lt;/a&gt;&amp;nbsp; This works great! I have come to realize I have data in columns that is less than 30 and greater than 30 is there an easy way to only show this? An example&amp;nbsp;&lt;SPAN&gt;column 1 has 50 rows and column 2 has 75 rows and column 3 has 28 rows and I want each row to have the last 30 rows of info nothing that is below 30 so if it has 29 or less data points I would like to delete it also. Thanks for the help!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Aug 2021 12:54:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412511#M66207</guid>
      <dc:creator>rmthomas</dc:creator>
      <dc:date>2021-08-25T12:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: Last 30 pieces for each row and delete the other data</title>
      <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412523#M66208</link>
      <description>&lt;P&gt;Try this version.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );

// create the example

dt = New Table( "Odd Lengths",
	New Column( "Column 1", Numeric, Continuous, Values( 1::50 ) ),
	New Column( "Column 2", Numeric, Continuous, Values( 1::75 ) ),
	New Column( "Column 3", Numeric, Continuous, Values( 1::28 ) )
);

Wait( 0 );

// process the data table

For( col = N Col( dt ), col &amp;gt; 0, col--,
	values = Column( dt, col ) &amp;lt;&amp;lt; Get Values;
	not missing = Sum( Not( Is Missing( values ) ) );
	If( not missing &amp;lt; 30,
		dt &amp;lt;&amp;lt; Delete Columns( Column( dt, col ) ),
		end = Contains( values, . ) - 1;
		If( end == -1, end = N Row( dt ) );
		view = end - 29 :: end;
		Column( dt, col ) &amp;lt;&amp;lt; Set Values( values[view] );
		
	);
);

dt &amp;lt;&amp;lt; Select Where( Row() &amp;gt; 30 ) &amp;lt;&amp;lt; Delete Rows;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Aug 2021 13:51:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412523#M66208</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2021-08-25T13:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: Last 30 pieces for each row and delete the other data</title>
      <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412532#M66209</link>
      <description>&lt;P&gt;&amp;nbsp;FWIW, here's yet another version using matrices:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

// Make a data table 'dt', to play with.
nr = 100;		// Number of rows
nc = 30;		// Number of columns
p = 0.7;		// Proportion of missing values
vals = J(nr*nc, 1, RandomInteger(0, 99));
makeMissing = RandomIndex(nr*nc, Floor(nr*nc*p));
vals[makeMissing] = .;
dt = AsTable(Shape(vals, nr, nc));
dt &amp;lt;&amp;lt; setName("Random Integers with some missing");

// Start here with the current table . . .
lastN = 30;														// Number of non-missing values to retain
dt = CurrentDataTable();
vals = dt &amp;lt;&amp;lt; getAsMatrix;
// Loop over columns . . .
newVals = [];
for(c=1, c&amp;lt;=NCol(vals), c++,
	NMV = vals[LocNonMissing(vals[0, c]), c];					// Non-mising values in this column
	if(NRow(NMV) &amp;gt;= lastN,										// Do we have enough non-missing values?
		lastNMV = NMV[(NRow(NMV) - lastN + 1)::NRow(NMV)];		// If so, get the 'lastN' of these  . . .
		newVals = newVals||lastNMV;								// . . . and add them into a new column
		);
);
dt2 = AsTable(newVals);
dt2 &amp;lt;&amp;lt; setName("Last "||Char(lastN)||" non-missing Random Integers");&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Aug 2021 14:08:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412532#M66209</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2021-08-25T14:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: Last 30 pieces for each row and delete the other data</title>
      <link>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412539#M66210</link>
      <description>&lt;P&gt;For the of future readers of this track, a correction to&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/5358"&gt;@Mark_Bailey&lt;/a&gt;&amp;nbsp;statement about his code when compared to the second piece of code from&amp;nbsp;@txnelson response needs to be clarified.&amp;nbsp; The two pieces of code will produce the same results if the assumption is made that once a missing value is found, that&amp;nbsp; no additional non missing values appear in the column.&amp;nbsp; &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/5358"&gt;@Mark_Bailey&lt;/a&gt;&amp;nbsp;code requires that the minimum number of non missing values must exist, where&amp;nbsp;@txnelson code does not&lt;/P&gt;</description>
      <pubDate>Wed, 25 Aug 2021 14:17:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Last-30-pieces-for-each-row-and-delete-the-other-data/m-p/412539#M66210</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2021-08-25T14:17:16Z</dc:date>
    </item>
  </channel>
</rss>

