String_Aggregation的摘要你好,
我该如何编码一个表,其中:
-根据字符和
-使用conact_items构建相应的单元格(例如,以分隔)。
-例如SQL代码“ SELECT sex,String_Agg(Name,',')as ConcatName FROM'Big Class'” GROUP BY sex
我知道通过循环可以做到这一点,但我想知道是否还有另一种更优雅的方式。
基于“大类”表,结果应如下所示:
性别ConcatName
f凯蒂,路易丝....
蒂姆(Tim),詹姆斯(James),...
非常感谢
脚本编写回复:摘要与String_Aggregation列表平台似乎需要连接汇总功能。这是在汇总表的每一行上使用单个循环的一种方法:
Names default to here(1);
dt = Open("$Sample_data/big class.jmp");
//Table with each unique sex and count
dtSum = (dt << Tabulate(
Add Table( Row Table( Grouping Columns( :sex ) ) )
)) << Make Into Data Table;
//blank column for concatenated names
dtSum << New Column("Names", Character, "Nominal");
//loop for each row/sex
for each row(dtSum,
// Use Eval and eval expression to insert the sex of the current
// row into the where expression of the other table
Eval(Eval Expr(
//fill in the names one row at a time, joining all values together
:Names = char( Concat Items(
//get names from main table matching the sex of the summary table
dt:name[ dt << Get Rows Where( dt:sex == Expr(:sex) ) ],
","
) )
))
);
回复:摘要与String_Aggregation这是使用JSL(JMP 15)的尝试。 也可以使用转换和合并列以交互方式完成。
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt1 = dt << Transpose( columns( :name ), By( :sex ), Output Table( "Test" ) );
dt1 << Combine Columns(
delimiter( ", " ),
Columns( 3 :: N Col( dt1 ) ),
Selected Columns are Indicator Columns( 0 ),
Column Name( "ConcatName" )
) << deletecolumns( 2, 4 :: N Col( dt1 ) );
回复:摘要与String_Aggregation@ih和@小姐已经为您提供了很好的答案,但是我已经在研究此解决方案,因此我想将其发布以展示通过JSL进行此操作的另一种方法。
利用子集在选项子集我们可以为每个子组获取一个表,然后从“名称”列中获取值并使用Concat物品()构建名称字符串。
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt_list = dt << Subset(
By( :sex ),
All rows,
Selected columns only( 0 ),
columns( :name, :age, :height, :weight ),
private
);
nt = New Table();
group_col_name = Word( 1, dt_list[1] << get name, "=" );
nt << New Column( group_col_name, character );
nt << New Column( "concat name" );
nt << add rows( N Items( dt_list ) );
For( i = 1, i <= N Items( dt_list ), i++,
Column( nt, group_col_name )[i] = Word( 2, dt_list[i] << get name, "= " );
nt:concat name[i] = Concat Items( dt_list[i]:name << get values, ", " );
);
For( i = 1, i <= N Items( dt_list ), i++,
Close( dt_list[1], nosave )
);
dt_list = Empty();
回复:摘要与String_Aggregation我不知道Subset(By()...)返回一个列表。 可能非常有用。 谢谢杰夫!
/马库斯