<?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 to replace empty values of numeric columns with the average column value in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39748#M23255</link>
    <description>&lt;P&gt;I didn't study your code in detail, but had the sense that you are doing more work than you need to. To make things more readable, you could consider using functions as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

// Only impute if more than n cells are missing
imputeContinuousCol =
Function({col, n}, {Default Local},
	values = col &amp;lt;&amp;lt; getValues;
	missingValuePos = Loc(IsMissing(values));
	if(NRow(missingValuePos) &amp;gt; n,
		values[Loc(IsMissing(values))] = Mean(values);
		col &amp;lt;&amp;lt; setValues(values);
		);
);

// Only impute if more than n cells are missing
imputeCharacterCol = 
Function({col, n}, {Default Local},
	values = col &amp;lt;&amp;lt; getValues;
	missingValuePos = Loc(values, "");
	if(NRow(missingValuePos) &amp;gt; n,
		values[Loc(values, "")] = "NA";
		col &amp;lt;&amp;lt; setValues(values);
		);
);

// Data table . . .
dt = DataTable("Big Class.jmp");
// List of column names that satidfy your imputation criteria . . .
imputeList = {"name", "sex", "height"};
// Loop over this list, and impute if necessary
for (c=1, c&amp;lt;=NItems(imputeList), c++,
	col = Column(dt, imputeList[c]);
	if(
		(col &amp;lt;&amp;lt; getModelingType) == "Continuous",
			imputeContinuousCol(col, 1),
		(col &amp;lt;&amp;lt; getDataType) == "Character",
			imputeCharacterCol(col, 1),
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Open 'Big Class', make some cells missing in the listed columns, then try it out.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 May 2017 12:39:34 GMT</pubDate>
    <dc:creator>ian_jmp</dc:creator>
    <dc:date>2017-05-30T12:39:34Z</dc:date>
    <item>
      <title>how to replace empty values of numeric columns with the average column value</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39349#M23010</link>
      <description>&lt;P&gt;&lt;SPAN&gt;good morning,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;someone can help me? ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;i would like to&amp;nbsp;replace empty values&amp;nbsp;of numeric&amp;nbsp;columns with&lt;SPAN class="short_text"&gt;&lt;SPAN&gt; the average column value.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;i tried this &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2"&gt;dt = &lt;/FONT&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;Current Data Table&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;();&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2"&gt;FILL = dt &amp;lt;&amp;lt; &lt;/FONT&gt;&lt;FONT color="#000080" face="Consolas" size="2"&gt;get column names&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;( Numeric );&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;For&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;( i = &lt;/FONT&gt;&lt;FONT color="#008080" face="Consolas" size="2"&gt;1&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;, i &amp;lt;= &lt;/FONT&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;N Items&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;( FILL ), i++,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2"&gt;FILL[i][dt &amp;lt;&amp;lt; &lt;/FONT&gt;&lt;FONT color="#000080" face="Consolas" size="2"&gt;get rows where&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;( &lt;/FONT&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;Is Missing&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;( FILL[i][] ) )] = &lt;/FONT&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;col mean&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;(FILL[i]);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance&lt;/P&gt;&lt;P&gt;Gianpaolo&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 12:24:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39349#M23010</guid>
      <dc:creator>gianpaolo</dc:creator>
      <dc:date>2017-05-18T12:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace empty values of numeric columns with the average column value</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39356#M23015</link>
      <description>&lt;P&gt;Please find one way to do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

// Make some test data
dt = NewTable("Test", NewColumn("Data", Numeric, Continuous, Formula(if(Mod(Row(), 2) == 0, RandomNormal()))));
dt &amp;lt;&amp;lt; addRows(20);
dt &amp;lt;&amp;lt; runFormulas;
Column(dt, "Data") &amp;lt;&amp;lt; deleteFormula;

// Impute missing cells with the mean of the others
Wait(3);
col = Column(dt, "Data");
values = col &amp;lt;&amp;lt; getValues;
values[Loc(IsMissing(values))] = Mean(values);
col &amp;lt;&amp;lt; setValues(values);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Probably it's good practice to delineate the values that were imputed by colouring their cells.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 15:19:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39356#M23015</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2017-05-18T15:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace empty values of numeric columns with the average column value</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39725#M23239</link>
      <description>hi Ian&lt;BR /&gt;first of all thanks for your help and suggestions. your script work very well&lt;BR /&gt;So.... I tried to integrate your script into a more big one, in order to reduce and simply DB.&lt;BR /&gt;&lt;BR /&gt;here the entire work&lt;BR /&gt;Names Default To Here( 1 );&lt;BR /&gt;clear globals();&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;// code to choose among all opened tables which one will be selected for the analysis&lt;BR /&gt;OpenDT = List();&lt;BR /&gt;For( i = 1, i &amp;lt;= N Table(), i++,&lt;BR /&gt;Insert Into( OpenDT, Data Table( i ) )&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;dlgDT = Dialog( "Select Data Table for analysis", VList( selectDT = List Box( OpenDT ) ), HList( Button( "OK" ), Button( "Cancel" ) ) );&lt;BR /&gt;&lt;BR /&gt;Show( dlgDT );&lt;BR /&gt;If( dlgDT["button"] == 1,&lt;BR /&gt;Remove From( dlgDT, N Items( dlgDT ) );&lt;BR /&gt;Eval List( dlgDT );&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Current Data Table( selectDT[1] );&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;// -----------------------------------------GUI -----------------------------------------//&lt;BR /&gt;cd = Column Dialog(&lt;BR /&gt;Title( " Data Preprocessing " ),&lt;BR /&gt;List= ColList( " Select Columns &amp;gt;&amp;gt; ", MinCol( 1 ) ),&lt;BR /&gt;HList( " % of Empty elements in the column -&amp;gt; (0-100) ", mypercent1 = EditNumber( 50 ) ),&lt;BR /&gt;HList( " % of Stagnant elements in the column -&amp;gt; (0-100) ", mypercent = EditNumber( 80 ) ),&lt;BR /&gt;VList(&lt;BR /&gt;numeric = Check Box( "Columns Numeric - Impute missing cells with the mean of the others", 1 ),&lt;BR /&gt;character = Check Box( "Columns character - Impute missing cells with N/A", 1 ),&lt;BR /&gt;&lt;BR /&gt;),&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;//declaration of variables&lt;BR /&gt;&lt;BR /&gt;impute_num_col = cd["numeric"]; // impute numerics&lt;BR /&gt;impute_char = cd["character"]; // impute characters&lt;BR /&gt;&lt;BR /&gt;percent = cd["mypercent"];&lt;BR /&gt;percent1 = cd["mypercent1"];&lt;BR /&gt;&lt;BR /&gt;mylist = cd["List"];&lt;BR /&gt;&lt;BR /&gt;dt= selectDT[1];&lt;BR /&gt;delcols = {};&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;numRows = nrows(dt);&lt;BR /&gt;for(i=1; maxi=Nitems(mylist), i&amp;lt;=maxi, i++,&lt;BR /&gt;if(nrows(loc(aslist(column(dt,mylist[i])&amp;lt;&amp;lt;getasmatrix),mode(column(dt,mylist[i])&amp;lt;&amp;lt;getasmatrix))) &amp;gt;= numRows * percent/100,&lt;BR /&gt;insert into(delcols, myList[i]);&lt;BR /&gt;,&lt;BR /&gt;if(Col N Missing(column(dt,mylist[i])) &amp;gt;= numRows * percent1/100,&lt;BR /&gt;insert into(delcols, myList[i]);&lt;BR /&gt;);&lt;BR /&gt;);&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;nitems = N Items( delcols );&lt;BR /&gt;colname = {};&lt;BR /&gt;colnamelist = {};&lt;BR /&gt;For( p = 1, p &amp;lt;= nitems, p++,&lt;BR /&gt;colname = Column( delcols[p] ) &amp;lt;&amp;lt; Get Name;&lt;BR /&gt;Insert Into( colnamelist, colname(p) );&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;dt &amp;lt;&amp;lt; delete columns (colnamelist);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;//OK! Columns Numeric - Impute missing cells with the mean of the others&lt;BR /&gt;If( impute_num_col == 1,&lt;BR /&gt;&lt;BR /&gt;cc = dt &amp;lt;&amp;lt; Get Column Names( Continuous );&lt;BR /&gt;ncols = N Items( cc );&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;For( i = 1, i &amp;lt;= ncols, i++,&lt;BR /&gt;values = Column( dt, cc[i] ) &amp;lt;&amp;lt; get values( 1 );&lt;BR /&gt;values[Loc( Is Missing( values ) )] = Mean( values );&lt;BR /&gt;Column( dt, cc[i] ) &amp;lt;&amp;lt; setvalues( values, cc[i] );&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;);&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;// Columns character - Impute missing cells with NA&lt;BR /&gt;If( impute_char == 1,&lt;BR /&gt;FILL = dt &amp;lt;&amp;lt; get column names( Character );&lt;BR /&gt;For( i = 1, i &amp;lt;= N Items( FILL ), i++,&lt;BR /&gt;FILL[i][dt &amp;lt;&amp;lt; get rows where( Is Missing( FILL[i][] ) )] = "NA";&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;but the problem is that the last part of script (#Columns character - Impute missing cells with NA#)&lt;BR /&gt;don't know why... doesn't work....&lt;BR /&gt;very strange because the same code works if isn't integrate in the job above.&lt;BR /&gt;&lt;BR /&gt;dt = Current Data Table();&lt;BR /&gt;FILL = dt &amp;lt;&amp;lt; get column names( Character );&lt;BR /&gt;For( i = 1, i &amp;lt;= N Items( FILL ), i++,&lt;BR /&gt;FILL[i][dt &amp;lt;&amp;lt; get rows where( Is Missing( FILL[i][] ) )] = "NA";&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;do you have idea why it is happening... ?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;thanks for collaboration</description>
      <pubDate>Mon, 29 May 2017 15:28:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39725#M23239</guid>
      <dc:creator>gianpaolo</dc:creator>
      <dc:date>2017-05-29T15:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace empty values of numeric columns with the average column value</title>
      <link>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39748#M23255</link>
      <description>&lt;P&gt;I didn't study your code in detail, but had the sense that you are doing more work than you need to. To make things more readable, you could consider using functions as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;NamesDefaultToHere(1);

// Only impute if more than n cells are missing
imputeContinuousCol =
Function({col, n}, {Default Local},
	values = col &amp;lt;&amp;lt; getValues;
	missingValuePos = Loc(IsMissing(values));
	if(NRow(missingValuePos) &amp;gt; n,
		values[Loc(IsMissing(values))] = Mean(values);
		col &amp;lt;&amp;lt; setValues(values);
		);
);

// Only impute if more than n cells are missing
imputeCharacterCol = 
Function({col, n}, {Default Local},
	values = col &amp;lt;&amp;lt; getValues;
	missingValuePos = Loc(values, "");
	if(NRow(missingValuePos) &amp;gt; n,
		values[Loc(values, "")] = "NA";
		col &amp;lt;&amp;lt; setValues(values);
		);
);

// Data table . . .
dt = DataTable("Big Class.jmp");
// List of column names that satidfy your imputation criteria . . .
imputeList = {"name", "sex", "height"};
// Loop over this list, and impute if necessary
for (c=1, c&amp;lt;=NItems(imputeList), c++,
	col = Column(dt, imputeList[c]);
	if(
		(col &amp;lt;&amp;lt; getModelingType) == "Continuous",
			imputeContinuousCol(col, 1),
		(col &amp;lt;&amp;lt; getDataType) == "Character",
			imputeCharacterCol(col, 1),
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Open 'Big Class', make some cells missing in the listed columns, then try it out.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 12:39:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/how-to-replace-empty-values-of-numeric-columns-with-the-average/m-p/39748#M23255</guid>
      <dc:creator>ian_jmp</dc:creator>
      <dc:date>2017-05-30T12:39:34Z</dc:date>
    </item>
  </channel>
</rss>

