你好 ,
我正在嘗試使用變量類型從每個 ID 的多行創建一個變量(type2)。 我想隱藏一張這樣的桌子:
到:
其中 type2 是:
如果 type 總是 A 則 type2=A;
如果類型始終為 B,則 type2=B;
如果 type 是 A 和 B 的組合,則 type2=C;
謝謝
有很多方法可以做到這一點。 我會計算類型在 ID 中出現的次數,如果它與 ID 在數據中出現的數量相同,那麼您就知道它是唯一的類型。
用公式總結:
Names Default To Here(1);
dt = New Table("From",
Add Rows(8),
New Column("ID", Numeric, "Nominal", Format("Best", 12), Set Values([1, 1, 1, 2, 2, 3, 3, 3])),
New Column("type", Character(16), "Nominal", Set Values({"A", "A", "B", "A", "A", "B", "B", "B"}))
);
dt_summary = dt << Summary(
Output table("To"),
Group(:ID),
N(:type),
Subgroup(:type),
Freq("None"),
Weight("None"),
statistics column name format("column"),
Link to original data table(0)
);
dt_summary << New Column("type2", Character, "Nominal",
<< Set Each Value(
If(:"type, A"n == 0, "B",
:"type, B"n == 0, "A",
"C"
)
)
);和其他使用 Col Sum 和 Select 重複行:
Names Default To Here(1);
dt = New Table("From",
Add Rows(8),
New Column("ID", Numeric, "Nominal", Format("Best", 12), Set Values([1, 1, 1, 2, 2, 3, 3, 3])),
New Column("type", Character(16), "Nominal", Set Values({"A", "A", "B", "A", "A", "B", "B", "B"}))
);
dt << New Column("type2", Character, Nomimal, Formula(
If(Col Sum(1, :ID) == Col Sum(1, :ID, :type),
:type,
"C"
)
));
wait(1);
dt:type2 << Delete Formula;
wait(1);
dt << Select Duplicate Rows(Match(:ID, :type2));<BR />wait(1);<BR />dt << Delete Rows;有關函數的更多信息,尤其是 Col Sum,請參見腳本索引。
你好,
看起來您的表格可能按 :ID 和 :type 排序。 如果是,或者如果您願意對其進行排序,您可以利用這一點並對 :type2 列使用(相對)簡單的公式:
If( :type[Col Min( Row(), :ID )] == :type[Col Max( Row(), :ID )],
:type,
"C"
)
乾杯,
布雷迪