你好,
我遇到一個問題,我有太多想要選擇的列,但我不想將它們全部鍵入以創建它們的新子集。
我有 200 列,全部以相同的字符串開頭。 我想知道是否有一種方法可以通過循環或某些命令來選擇這些列。
我嘗試過使用以下行:
cols = dt << Get Column Names(Starts With("Year"));
但是,如果只獲取每個列名稱,並且我確實有一些列不以“年”開頭,甚至名稱中不包含“年”。 現在我正在製作我想要的列的新子集:
dt2 = dt << Subset(
All rows,
columns(
:Time,
:Day,
:Name("Year 1"),
:Name("Year 2"),
:Name("Year 3"),
.
.
.
etc.
),
output table name("All Years")
);
我的表中還有許多其他列,但我只想將它們分開。 請幫忙!
這段代碼就能解決問題。不錯的博客戴夫,順便說一句!
dt = New Table( "Year Example",
Add Rows( 3 ),
New Column( "ABC", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),
New Column( "DEF", Numeric, Continuous, Format( "Best", 12 ), Set Values( [4, 5, 6] ) ),
New Column( "Year 1", Numeric, Continuous, Format( "Best", 12 ), Set Values( [7, 8, 9] ) ),
New Column( "Year 2", Numeric, Continuous, Format( "Best", 12 ), Set Values( [10, 11, 12] ) ),
New Column( "Year 3", Numeric, Continuous, Format( "Best", 12 ), Set Values( [13, 14, 15] ) )
);
col_list = dt << get column names(string);
for (i = nitems(col_list), i > 0, i--,
if (!contains(col_list[i], "Year"),
remove from(col_list, i);
);
);
dt << Subset(
Output Table( "Year Subset" ),
All rows,
columns( eval(col_list) )
);