在將幾個重要列分組後,我想隱藏“閣樓”中剩餘的列。
如何取得所有未分組列的清單?
取得所有列的列表,取得分組列的列表,並從列列表中刪除分組列。
Names Default To Here(1);
// Setup
dt = Open("$SAMPLE_DATA/Cities.jmp");
dt << group columns("xy", {:X, :y});
dt << group columns("pollutants", :Ozone :: :Lead);
collist = dt << Get Column Names("String");
grouped_list = {};
For Each({groupname}, dt << get column groups names,
c = dt << get column group(groupname);
groupcols = Transform Each({cr}, c, cr << get name);
Insert Into(grouped_list, groupcols);
);
nongrouped = Set Difference(collist, grouped_list);
For Each({colname}, nongrouped,
Column(dt, colname) << Hide(1);
);
wait(0);
透過字串這種迂迴的方式是合理的:)
我想知道為什麼有必要這樣做…
Names Default To Here(1);
// Setup
dt = Open("$SAMPLE_DATA/Big Class.jmp");
dd= dt << group columns("xy", {:X, :y});
ee= dt << group columns("pollutants", :Ozone :: :Lead);
Type(ee)
collist = dt << Get Column Names();
grouped_list = {};
groups= dt << get column groups names;
For Each({groupname},groups,
groupcols= dt << get column group(groupname);
Insert Into(grouped_list, groupcols);
);
Show(grouped_list); // list of names with ":" {:OZONE, :CO, :SO2, :NO, :PM10, :Lead, :X, :Y};
show(collist);// list of names without ":" {city, Latitude, Longitude, ... }
nongrouped = Set Difference(collist, grouped_list); // ?!?! {city, CO, Latitude, Lead, Longitude, Max deg. F Jan, NO, OZONE, PM10, POP,"pop- m"n, Region, SO2, State, X, Y}
需要了解的重要細節:
- << group columns的回傳值是一個字串,而不是一個列組。
<< get column group returns a list of columns, not a column group
- 重要提示:此列清單以“:”開頭列出列名。
- 比較:get column names() 傳回一個沒有前導「:」的名稱清單。
- 這兩個列表彼此不相容
變通方法:
convert to list of columns = Function ({myColList},
Transform each({col},myColList, Name Expr(As column(dt, col)))
);
grouped_list = convert to list of columns(grouped_list); // looks like before, but is something completely different
collist = convert to list of columns(collist); //{:city, :Latitude, :Longitude, ... }
nongrouped = Set Difference(collist, grouped_list);
Show (nongrouped) // {:city, :Latitude, :Longitude, :Max deg. F Jan, :POP, :"pop- m"n, :Region, :State};
處理列資料時,我唯一關心的就是將它們全部轉換為字串,因為這可以避免很多不必要的麻煩。在我看來,一開始就嘗試使用字串以外的任何格式來處理列資料基本上是沒有意義的,因為你可以隨時將字串轉換為其他「列格式」。而且,如果出於某種原因需要從字串以外的格式開始,根據我的經驗,這樣做是得不償失的。