<?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 perform calculations using selected columns in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/269963#M52548</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am struggling to find a good scripting solution for this.&amp;nbsp; I would like to select a few columns and then perform calculations across them.&amp;nbsp; The selected columns are flexible, sometimes it could be 2 but sometimes it could be 10+ columns to perform this calculation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;new column with formula&amp;nbsp;sqrt(sum(A^4, B^4, C^4)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At this moment, I have the script to extract selected column. But once they are in a list with column name, I don't know how to sum up the column using the formula I have above...&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
cols = dt &amp;lt;&amp;lt; get selected columns;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you so much for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 31 May 2020 20:09:24 GMT</pubDate>
    <dc:creator>KellyT</dc:creator>
    <dc:date>2020-05-31T20:09:24Z</dc:date>
    <item>
      <title>perform calculations using selected columns</title>
      <link>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/269963#M52548</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am struggling to find a good scripting solution for this.&amp;nbsp; I would like to select a few columns and then perform calculations across them.&amp;nbsp; The selected columns are flexible, sometimes it could be 2 but sometimes it could be 10+ columns to perform this calculation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;new column with formula&amp;nbsp;sqrt(sum(A^4, B^4, C^4)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At this moment, I have the script to extract selected column. But once they are in a list with column name, I don't know how to sum up the column using the formula I have above...&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();
cols = dt &amp;lt;&amp;lt; get selected columns;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you so much for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 20:09:24 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/269963#M52548</guid>
      <dc:creator>KellyT</dc:creator>
      <dc:date>2020-05-31T20:09:24Z</dc:date>
    </item>
    <item>
      <title>Re: perform calculations using selected columns</title>
      <link>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/269972#M52549</link>
      <description>&lt;P&gt;Here is, using JSL,&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; New Column( "Calc",
	formula(
		cols = dt &amp;lt;&amp;lt; get selected columns;
		mat = [];

		For( i = 1, i &amp;lt;= N Items( cols ), i++,
			mat = mat || Column( cols[i] )[Row()]
		);
		mat = Sqrt( Sum( mat ^ 4 ) );
	)
);

dt:calc &amp;lt;&amp;lt; delete formula;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;how I would create a formula column to handle your issue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 23:10:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/269972#M52549</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-05-31T23:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: perform calculations using selected columns</title>
      <link>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/270046#M52557</link>
      <description>&lt;P&gt;Thank you Jim! Your script is very helpful as always.&amp;nbsp; To help me understand JSL better, can you please elaborate what this line of code does?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;mat = mat || Column( cols[i] )[Row()]&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jun 2020 15:44:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/270046#M52557</guid>
      <dc:creator>KellyT</dc:creator>
      <dc:date>2020-06-01T15:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: perform calculations using selected columns</title>
      <link>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/270051#M52559</link>
      <description>&lt;P&gt;I chose the method to use, based upon the very nice capabilities the Matrix operators have in JSL.&amp;nbsp; The statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;mat = Sqrt( Sum( mat ^ 4 ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is an illustration of how nice and concise the calculations can be made using matrices.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But to use matrix operators, the data must be in a matrix. So for each row, the matrix Mat is first set to being an empty matrix&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;mat = [];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then the For() loop takes each of the columns specified in the &amp;lt;&amp;lt; Get Selected Columns and adds the value for that column for the given row, to the matrix "Mat" as a new element in the matrix.&amp;nbsp; It is "Concatenating" the value of the column for the current row to the matrix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;mat = mat || Column( cols[i] )[Row()]&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jun 2020 16:06:55 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/perform-calculations-using-selected-columns/m-p/270051#M52559</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-06-01T16:06:55Z</dc:date>
    </item>
  </channel>
</rss>

