<?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: Rename columns with Regex in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109802#M39704</link>
    <description>&lt;P&gt;This solution on changing the names doesn't use regular expressions, but it is a very simple piece of code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 2 );
dt = Current Data Table();
colNamesList = dt &amp;lt;&amp;lt; get column names( string );

For( i = 1, i &amp;lt;= N Items( colNamesList ), i++,
	If( Word( 3, colNamesList[i], "_" ) == "",
		Column( colNamesList[i] ) &amp;lt;&amp;lt; set name(
			Word( 1, colNamesList[i], "_" ) || "_NYC_" || Word( 2, colNamesList[i], "_" )
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 11 Feb 2019 05:40:25 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2019-02-11T05:40:25Z</dc:date>
    <item>
      <title>Rename columns with Regex</title>
      <link>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109791#M39702</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a list of a huge number of columns to stack that have column format name of TEST_BOUROUGH_GENDER, as shown below. Now the BOUROUGH can be missing from the column name and that imples the value from the column is from all the BOUROUGHs (ie NYC). I stacking all of the TEST_BOUROUGH_GENDER column names and then creating new columns called test, location and gender (see below). When I run into the case of a column that is missing a BOUROUGH name I would like to add NYC to that column name. I would also prefer if this is could be done with regular expressions. Any thoughts or input is greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;column format name&lt;/STRONG&gt;: CALC1_BROOKLYN_M, CALC2_QUEENS_F, CALC2_M, ..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ref = Current Data Table();&lt;/P&gt;&lt;P&gt;stat stack &amp;lt;&amp;lt; ref stack(Source Label Column("Label"), Stacked Data Column("Data"));&lt;/P&gt;&lt;P&gt;stat stack &amp;lt;&amp;lt; New Column("test", Formula(Right( :Label, 5 )));&lt;/P&gt;&lt;P&gt;stat stack &amp;lt;&amp;lt; New Column("location", Formula(Regex( :Label, "_(.*)_", "\1" )));&lt;/P&gt;&lt;P&gt;stat stack &amp;lt;&amp;lt; New Column("gender", Formula(Right( :Label, 1)))&lt;/P&gt;</description>
      <pubDate>Mon, 11 Feb 2019 04:20:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109791#M39702</guid>
      <dc:creator>tarkan_bih</dc:creator>
      <dc:date>2019-02-11T04:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns with Regex</title>
      <link>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109802#M39704</link>
      <description>&lt;P&gt;This solution on changing the names doesn't use regular expressions, but it is a very simple piece of code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 2 );
dt = Current Data Table();
colNamesList = dt &amp;lt;&amp;lt; get column names( string );

For( i = 1, i &amp;lt;= N Items( colNamesList ), i++,
	If( Word( 3, colNamesList[i], "_" ) == "",
		Column( colNamesList[i] ) &amp;lt;&amp;lt; set name(
			Word( 1, colNamesList[i], "_" ) || "_NYC_" || Word( 2, colNamesList[i], "_" )
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Feb 2019 05:40:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109802#M39704</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2019-02-11T05:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns with Regex</title>
      <link>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109818#M39720</link>
      <description>&lt;P&gt;This should work as long as Bourough don't contains an underscore.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = New Table("test",
    Add Rows(3),
    New Column("Label", Character, "Nominal", Set Selected, Set Values({"CALC1_BROOKLYN_M", "CALC2_QUEENS_F", "CALC2_M"}))
);

dt &amp;lt;&amp;lt; New Column("test", Formula(Left(:Label, 5)));
dt &amp;lt;&amp;lt; New Column("location", Formula(If(Is Missing(Regex(:Label, "_(.*)_")), "NYC", Regex(:Label, "_(.*)_", "\1"))));
dt &amp;lt;&amp;lt; New Column("gender", Formula(Right(:Label, 1)));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Feb 2019 11:37:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109818#M39720</guid>
      <dc:creator>ms</dc:creator>
      <dc:date>2019-02-11T11:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns with Regex</title>
      <link>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109855#M39735</link>
      <description>&lt;P&gt;This is what I want! However when I try to put this in my script it is crashing. The values I used in my example (TEST_BOUROUGH_GENDER) are not real due to IP issues but mearly are used as an example. Also after the last delimiter "_" the column will either start M or P. Is there a way with startswith function to include two seperate characters it can start with?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;allColNames = dt &amp;lt;&amp;lt; getcolumnnames;
pulledCols = {};

Ncols = N items(allColNames);
for(i = 1, i &amp;lt;= Ncols, i++,
	cname = Column(allColNames[i]) &amp;lt;&amp;lt; get Name;
	if(startswith(Word(5,cname , "_"),"M"),
		Column(cname) &amp;lt;&amp;lt; set name(
			Word(1, cname, "_") || "_"|| Word(2, cname, "_") || "_" || Word(3, cname, "_") || "_" || Word(4, cname, "_") ||"_ALL_" || Word(5,cname,"_")
		)
	),
	if(startswith(Word(5,cname , "_"),"P"),
		Column(cname) &amp;lt;&amp;lt; set name(
			Word(1, cname, "_") || "_"|| Word(2, cname, "_") || "_" || Word(3, cname, "_") || "_" || Word(4, cname, "_") ||"_ALL_" || Word(5,cname,"_")
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Feb 2019 16:03:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109855#M39735</guid>
      <dc:creator>tarkan_bih</dc:creator>
      <dc:date>2019-02-11T16:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns with Regex</title>
      <link>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109870#M39740</link>
      <description>&lt;P&gt;If I am understanding what you want, you can use an OR clause to get what you want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;allColNames = dt &amp;lt;&amp;lt; getcolumnnames;
pulledCols = {};

for(i = 1, i &amp;lt;= N items(allColNames), i++,
	cname = Column(allColNames[i]) &amp;lt;&amp;lt; get Name;
	if(startswith(Word(5,cname , "_"),"M") | startswith(Word(5,cname , "_"),"P"),
		Column(cname) &amp;lt;&amp;lt; set name(
			Word(1, cname, "_") || "_"|| Word(2, cname, "_") || "_" || Word(3, cname, "_") || "_" || Word(4, cname, "_") ||"_ALL_" || Word(5,cname,"_")
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Feb 2019 17:20:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109870#M39740</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2019-02-11T17:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns with Regex</title>
      <link>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109871#M39741</link>
      <description>&lt;P&gt;This fixes everything and now it works! Thanks for all the support. One final improvement I see is that instead of having a long string of concatentation with the word function is there a way to return everything before the delimiter? So for example is there a way to write it as &amp;lt;all values before Word(4, cname, "_")&amp;nbsp; including the underscores&amp;gt; || "_ALL_" || Word(5, cname, "_"). So basically I want column name string before the 4th delimiter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Column(cname) &amp;lt;&amp;lt; set name(
	Word(1, cname, "_") || "_"|| Word(2, cname, "_") || "_" || Word(3, cname, "_") || "_" || Word(4, cname, "_") ||"_ALL_" || Word(5,cname,"_")
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Feb 2019 17:30:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Rename-columns-with-Regex/m-p/109871#M39741</guid>
      <dc:creator>tarkan_bih</dc:creator>
      <dc:date>2019-02-11T17:30:20Z</dc:date>
    </item>
  </channel>
</rss>

