clone a column<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />Hi All,
I want to clone a column in the data table (make a new identical column in all but itâs name). Is there a more sleek (and robust) way of doing this or do I need to extract the values and all properties and assign them to the new column as such:
dt3 = Open( "$SAMPLE_DATA/Cars.jmp" );
// add a column property
dt3:Make << Set Property(
"Value Colors",
{"Acura" = -13977687, "Audi" = -3780931, "BMW" = -4222943, "Buick" = -
13596966, "Cadillac" = -2211217, "Chevrolet" = -10562780, "Chrysler" = -
13156647, "Daihatsu" = -2078390, "Dodge" = -13182413, "Eagle" = -9549600,
"Ford" = -2334147, "Geo" = -13772446, "Honda" = -9282864, "Hyundai" = -
6995852, "Infiniti" = -1524612, "Isuzu" = -9458080, "Jeep" = -14452073,
"Lexus" = -6391856, "Lincoln" = -2745505, "Mazda" = -10199751, "Mercedes" =
-7150697, "Mercury" = -10513726, "Mitsubishi" = -8381519, "Nissan" = -
3502441, "Oldsmobile" = -3615440, "Peugeot" = -13925307, "Plymouth" = -
11502354, "Pontiac" = -7449196, "Renault" = -9229791, "Saab" = -4074344,
"Saturn" = -13050224, "Subaru" = -12565885, "Suzuki" = -2068529, "Toyota" =
-4494272, "Volkswagen" = -11824110, "Volvo" = -8734293, "Yugo" = -13849421}
) << color cell by value;
// clone the column
clone_column = dt3:Make << Get script;
clone_column;
Re: clone a columnNo, that interpretation is completely wrong.
Scripting JMP usually involves using built-in JMP objects like data tables, data columns, or analysis platforms. You use these objects by sending a message to the object that indicates the desired action. The general form is shown here in both versions: using the actual JMP function and using the equivalent operator.
Send( object, message );
object << message;
The object is represented by a reference value. The message is represented by name. This example is interpreted in the following way:
dt3:Make << Get Script
Send the message Get Script to the object referred to as dt3:Make. This reference is uses the form table:column. A reference to a data table was previously stored in the variable dt3. The reference to the column is by name.
The message Get Script may be sent to any JMP object. The 'getter' method behind this message is over-ridden by each object to return the script that will re-produce the original object. That is, it returns the code that will clone the object. So you can always use the same message to get the unique code for any object.
Re: clone a columnThanks for the explanation and the quick reply. I guess that Get is the name of the script that is 'built-in' to jmp and is run. Otherwise how would it know to clone the column as opposed to performing some other operation. I've learned a lot from your reply.
Re: clone a columnLet's work it out in the order in which the expression will be evaluated.
dt3:Make << Get script
The message will result in JMP returning the JSL code that would create the same column.
Eval(dt3:Make << Get script)
The JSL code is evaluated resulting in a new data column that is a copy of the original :Make column. The Eval() function will also return a reference to that new column.
Eval(dt3:Make << Get script) << set name("Clone");
Finally, send the new column the message to change the name of the new column using the object reference.
Re: clone a columnHi MS, Would you mind explaining how this command clones the selected column in a dt? Eval(dt3:Make << Get script) << set name("Clone"); I realize this is not normally the kind of question asked here and it was 5 years ago but this is the most opaque command I have seen in any programming language I have used. I works great however!Re: clone a columnThanks MS ,
It is true that colors are not very useful in a private table.
the thing is that I am writing an algorithm based on a heuristic process. this means that I need to have a script that would do what i would if i had to "read" the data lines one by one my self and interpret it (here is where i need the colors). on the other hand the script needs to be fast when running so i need to bounce back and fourth between tables I see and fast running ones (private). this transition between visual and private tables introduces robustness challenges while scripting across a few open tables.
ron
Re: clone a columnI'm really not sure what value there is in keeping column properties like value colors in a private table. If the data is all you care about (typically true for private tables), the simpler method below could be used.:
dt3 << New Column("Make Clone", Formula(dt3:Make))
Re: clone a columnthanks Dave@Pega-Analytics txnelson ,
this is what i was looking for. i didn't expect duplicating a column to be so cumbersome.
the following is my final script:
Names Default To Here( 1 );
dt3 = Open( "$SAMPLE_DATA/Cars.jmp" , private);
// clone into the same table
Eval(Parse("(dt3<<"||char(dt3:Make << Get script)||") << set name(\!"Clone\!");"));
// proof that the column was cloned - check log
dt3:Clone << get script;
close (dt3, no save);
best,
ron
Re: clone a columnHere is a completed example of one way to prepend the data table pointer:
Eval(Parse("(dtxxx<<"||char(dt3:Make << Get script)||") << set name(\!"Clone\!");"));
Re: clone a columnYou can make it more robust by editing the script variable prior to evaluation i.e. prepend "dt << " to the expression.
Re: clone a columnthe robustness issue came when using private tables since the clone was sent to another table which was not private.
Re: clone a columnThanks MS ,
i am surprised there is no jsl command for duplicating an existing column. in addition, if dt3 is not the active table it creates the new column in an other table.
Re: clone a columnThat looks very sleek to me. Have you experienced any problems with robustness?
If you do not want to waste memory on a variable it can be simplified into a oneliner:
Eval(dt3:Make << Get script) << set name("Clone");
For cloning multiple columns , I would consider making a subset of these, rename the columns and update with the original data table.