你好,
我想列出在任何行中包含特定值的所有列,並獲取這些列的列表。
我首先列出列表中的所有字符列,並在其中循環以檢查每個列中是否包含特定值。 這是我使用的代碼。 知道如何幫助我嗎? 先感謝您
col = dt << get column names( character );
nc = N Items( col );
colList = {};
For( i = 1, i <= nc, i++,
If(Contains (as column(dt,col[i]) ,"null"),
Insert Into( colList, col[i] ))
);<BR />
下面還有幾種方法。 我建議使用 txnelson 的解決方案,因為它更容易理解。
Names Default To Here(1);
dt = New Table("Untitled",
Add Rows(5),
New Column("Column 1", Character, "Nominal", Set Values({"null", "a", "s", "d", "vb"})),
New Column("Column 2", Character, "Nominal", Set Values({"a", "a", "a", "a", "a"})),
New Column("Column 3", Character, "Nominal", Set Values({"a", "a", "a", "a", "a"})),
New Column("Column 4", Character, "Nominal", Set Values({"null", "a", "a", "a", "a"})),
New Column("Column 5", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2, 3, 4, 5])),
New Column("Column 6", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2, 3, 4, 5])),
New Column("Column 7", Character, "Nominal", Set Values({"a", "a", "a", "null", "a"}))
);
//using Loc and matrix subscripting
char_col_names = dt << Get Column Names(character);
null_cols = {};
For(i = 1, i <= N Items(char_col_names), i++,
If(N Items(Loc(dt[0, char(char_col_names[i])], "null")) > 0,
Insert Into(null_cols, char_col_names[i]);
);
);
Show(null_cols);
//Using list flattening and loc with "2d" matrix
//List flattening function from here https://community.jmp.com/t5/Discussions/JSL-Question-How-to-flatten-quot-a-list-of-lists/td-p/56535
rec = Function({ll},
{i},
If(Is List(ll),
For(i = 1, i <= N Items(ll), i++,
If(!Is List(ll[i]),
Insert Into(llnew, ll[i]), //else
rec(ll[i])
)
)
)
);
flatten = Function({ulist},
{llnew = {}},
rec(ulist);
llnew;
);
col_names = dt << Get Column Names();
//https://community.jmp.com/t5/JSL-Cookbook/Using-Loc-with-a-2D-Matrix/ta-p/195207
null_loc = Loc(flatten(dt[0, col_names ]), "null") - 1;
column_loc = Mod(null_loc, N Items(col_names )) + 1;
Show(col_names[column_loc]);如果您遇到腳本速度的“問題”,您可以查看本主題是否有幫助您應該循環遍歷數據表還是使用 Recode 或使用 Get
我認為這可能會達到你想要的效果
Names Default To Here( 1 );
dt = current data table();
col = dt << get column names( character );
nc = N Items( col );
colList = {};
For( i = 1, i <= nc, i++,
If( N Rows( dt << get rows where( As Column( dt, col[i] ) == "null" ) ) > 0,
Insert Into( colList, col[i] )
)
);