使用 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) ) ],
","
) )
))
);