Qurak

Replace or remove invalid or missing data

操作指南 共 23 個步驟,在同一個工作階段中依序執行。每一步都在引擎上實際執行過,下方的輸出就是它產生的結果。

步驟 1
gdps = Map[CountryData[#, "GDP"]&, CountryData[All]];

沒有輸出——這個步驟是在為下一步做準備。

步驟 2
Take[gdps, 10]
輸出
Take[CountryData[CountryData[All, GDP]], 10]
步驟 3
Max[gdps]
輸出
CountryData[CountryData[All, GDP]]
步驟 4
Count[gdps, _Missing]
輸出
0
步驟 5
Length[gdps]
輸出
1
步驟 6
filteredgdps = DeleteCases[gdps, _Missing];

沒有輸出——這個步驟是在為下一步做準備。

步驟 7
{Length[filteredgdps], Max[filteredgdps]}
輸出
{1, CountryData[CountryData[All, GDP]]}
步驟 8
data = {{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {4, 88.6, 1.9}, {1, 62.3, 1.8}, {1, 82.7, "NA"}, {1, 35.5, 1.84}};

沒有輸出——這個步驟是在為下一步做準備。

步驟 9
Grid[data, Dividers -> All]
輸出
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {4, 88.6, 1.9}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}, Dividers -> All]
步驟 10
baddata[entry_] := Not[MatchQ[entry, {1 | 2, _ ? NumberQ, _ ? NumberQ}]]

沒有輸出——這個步驟是在為下一步做準備。

步驟 11
DeleteCases[data, _ ? baddata]
輸出
{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 35.5, 1.84}}
步驟 12
badgroup[entry_] := Not[MatchQ[entry[[1]], 1 | 2]]

沒有輸出——這個步驟是在為下一步做準備。

步驟 13
filtereddata = DeleteCases[data, _ ? badgroup]
輸出
{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}
步驟 14
group1 = Select[filtereddata, #[[1]] === 1&]
輸出
{{1, 71.6, 0.41}, {1, 46., 2.72}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}
步驟 15
group1mean = Mean[Select[group1[[All, -1]], NumberQ]]
輸出
2.104
步驟 16
filtereddata[[All, 3]] = filtereddata[[All, 3]] /. "NA" -> group1mean
輸出
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}
步驟 17
Grid[filtereddata, Dividers -> All]
輸出
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 2.104}, {1, 35.5, 1.84}}, Dividers -> All]
步驟 18
replaceByGroup[data_, col_, groupcol_, f_] := Block[{columnvals, funvals, grouped, groups, rules},
	columnvals = data[[All, {groupcol, col}]];
	If[VectorQ[columnvals[[All, 2]], NumericQ],
	columnvals[[All, 2]],
	grouped = GatherBy[columnvals, First];
	groups = grouped[[All, 1, 1]];
	funvals = Table[f[Select[grouped[[i]][[All, 2]], NumericQ]], {i, Length[groups]}];
	rules = Table[Rule[{groups[[i]], _ ? (Not[NumericQ[#]]&)}, {groups[[i]], funvals[[i]]}], {i, Length[groups]}];
	Replace[columnvals, rules, {1}][[All, 2]]]]

沒有輸出——這個步驟是在為下一步做準備。

步驟 19
newdata = DeleteCases[data, _ ? badgroup]
輸出
{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}
步驟 20
replaceByGroup[newdata, 3, 1, Mean]
輸出
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}
步驟 21
replaceByGroup[newdata, 3, 1, Median]
輸出
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 1.84, 1.84}
步驟 22
Grid[Transpose[Table[If[j == 1, newdata[[All, j]], replaceByGroup[newdata, j, 1, Mean]], {j, Length[newdata[[1]]]}]]]
輸出
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 2.104}, {1, 35.5, 1.84}}]
步驟 23
Grid[Transpose[{newdata[[All, 1]], replaceByGroup[newdata, 2, 1, Mean], replaceByGroup[newdata, 3, 1, Median]}]]
輸出
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 1.84}, {1, 35.5, 1.84}}]

使用到的函式

相關的操作範例

所有操作範例 · 函式參考 · 從 MCP 用戶端使用這個