Highlighted

## Transposing a table with custom arguments

I am trying to transpose a table with a known Label column name, but Transpose column names that are only known at runtime. I have been trying for hours to figure out how to do this, but the task is greatly complicated by JSL mistaking the Transpose command for the matrix Transpose function and he Columns() argument to it for the function that gets the names of all columns in the table. In the following code, the list columns is populated with the names of the columns I want to transpose.

//Create the transpose expression

transposeExpr = Expr(Transpose(Columns(), Label(:Wt or Rd)));

//Pull the Columns argument expression out of it (this keeps me from having to create it myself and have it mistaken for the Columns() function)

columnsExpr = Arg(transposeExpr, 1);

For(i = 1, i <= numColumns, i++,

Insert Into(columnsExpr, Name Expr(As Column(colNames)));

);

Remove From(transposeExpr, 1);

Insert Into(transposeExpr, Name Expr(columnsExpr), 1);

cdt = Current Data Table();

cdt << Transpose(Name Expr(columnsExpr), Label(:Wt or Rd));

However, this still doesn't appear to work because columnsExpr still evaluates to a list of names of all the columns in the current table, rather than the Columns argument with actual references to the table. (I think it interprets these references are arguments to Columns the function)

Can anyone please explain what I am doing wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

## Re: Transposing a table with custom arguments

Not sure if I understand exactly what you want to do, but maybe this example would be helpful, assuming the fitness data is current.

collist = {:Age, :Oxy, :Runtime};

Data Table( "Fitness" ) << Transpose(

columns( eval(collist)),

Label( :Name ),

Output Table( "Transpose of Fitness" )

)

4 REPLIES 4
Highlighted pmroz
Super User

## Re: Transposing a table with custom arguments

When I have trouble like this I resort to building a string dynamically, and executing it with eval(parse()).  It's somewhat brute force, but I get tied up in knots with all of the various functions like expr, eval, parse, name expr, etc.

This should do it:

transposeStr = "cdt << Transpose(Columns(";

For(i = 1, i <= numColumns, i++,

transposeStr = transposeStr || ":" || colNames[i] || ", ";

);

// Remove the last comma

clen = length(transposeStr);

transposeStr = substr(transposeStr, 1, (clen - 2));

transposeStr = transposeStr || "), Label(:Wt or Rd))";

eval(parse(transposeStr));

Highlighted

## Re: Transposing a table with custom arguments

Not sure if I understand exactly what you want to do, but maybe this example would be helpful, assuming the fitness data is current.

collist = {:Age, :Oxy, :Runtime};

Data Table( "Fitness" ) << Transpose(

columns( eval(collist)),

Label( :Name ),

Output Table( "Transpose of Fitness" )

)

Highlighted

## Re: Transposing a table with custom arguments

This appears to work. I do this:

colList = List();

For(i = 1, i <= numColumns, i++,

Insert Into(colList, Column(colNames));

);

cdt = Current Data Table();

cdt << Transpose(Columns(Eval(colList)), Label(:Wt or Rd));

Why do I have to cal Eval on colList in the final message? What is the difference between Column() and As Column()?

Highlighted

## Re: Transposing a table with custom arguments

1. Transpose( Columns() .... ) expects a bunch of column names separated by commas, as you would fill in manually. It does not expect the name of a list and doesn't know how to extract the info from the name of the list containing the column names of interest. The eval function bridges the gap. This is how I view the issue but someone else may have a more technically precise way of explaining it.

2. Column() versus As Column: I'm not sure how they differ. The short examples in the docs make them seem interchangeable in function. I'd be interested someone can point to differences between them.
{"ft_lang_label":"__label__en","ft_lang_prob":0.86400646,"math_prob":0.89145386,"size":3491,"snap":"2020-10-2020-16","text_gpt3_token_len":825,"char_repetition_ratio":0.1425294,"word_repetition_ratio":0.18101934,"special_character_ratio":0.25379547,"punctuation_ratio":0.1634183,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96412754,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-26T17:07:31Z\",\"WARC-Record-ID\":\"<urn:uuid:407cb1fb-34fb-40d2-af84-eff0f47ddfbe>\",\"Content-Length\":\"596473\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:752f13a5-0323-45f4-9c4c-da41c10d3e85>\",\"WARC-Concurrent-To\":\"<urn:uuid:9463d017-4147-4049-864e-ef2532423c99>\",\"WARC-IP-Address\":\"208.74.205.23\",\"WARC-Target-URI\":\"https://community.jmp.com/t5/Discussions/Transposing-a-table-with-custom-arguments/td-p/8076?trMode=source\",\"WARC-Payload-Digest\":\"sha1:J64HZ2WYE4GOD3COEI732QNIF2FUNYNM\",\"WARC-Block-Digest\":\"sha1:PLCP4G7ERXY7ZKBAUID4RNZ5YANNK43M\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146414.42_warc_CC-MAIN-20200226150200-20200226180200-00061.warc.gz\"}"}