使用 String_Aggregation 进行摘要你好,
我如何编码一个表:
- 根据字符分组
- 使用 conact_items 构建相应的单元格(例如用 , 分隔)。
- 就像 SQL 代码“SELECT sex, String_Agg(Name,', ') as ConcatName FROM 'Big Class'” GROUP BY sex
我知道这可以通过循环实现,但我想知道是否还有另一种更优雅的方式。
根据“Big Class”表,结果应如下所示:
性别 ConcatName
凯蒂、路易丝……
我蒂姆、詹姆斯……
非常感谢
数据存取回复:String_Aggregation 的总结谢谢杰夫,
您的解决方案也运行得很快,我也不知道 Subset(by()) 命令。
回复:String_Aggregation 的总结谢谢女士,
这是一种非常快速的方法。 我基于一个巨大的日志文件尝试了它,它在几秒钟内就可以工作!
回复:String_Aggregation 的总结谢谢我,
我在一些表上尝试了您的方法,一切正常。 有时它需要一点时间(取决于桌子的大小)。
回复:String_Aggregation 的总结感谢所有方法。
他们都工作得很好!
回复:String_Aggregation 的总结我不知道 Subset( By()...) 会返回一个列表。 可以非常有用。 谢谢杰夫!
/马库斯
回复:String_Aggregation 的总结@ih和@多发性硬化症已经给了你很好的答案,但我已经在研究这个解决方案,所以我想我会发布它来展示另一种方法来做到这一点,通过 JSL。
利用子集依据选项子集我们可以为每个子组获取一个表,然后从 Name 列获取值并使用连接项目()构建名称字符串。
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 的总结这是使用 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 的总结似乎制表平台需要一个连接汇总功能。下面是在汇总表的每一行上使用单个循环的一种方法:
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) ) ],
","
) )
))
);