操作指南 共 23 個步驟,在同一個工作階段中依序執行。每一步都在引擎上實際執行過,下方的輸出就是它產生的結果。
gdps = Map[CountryData[#, "GDP"]&, CountryData[All]];沒有輸出——這個步驟是在為下一步做準備。
Take[gdps, 10]Take[CountryData[CountryData[All, GDP]], 10]Max[gdps]CountryData[CountryData[All, GDP]]Count[gdps, _Missing]0Length[gdps]1filteredgdps = DeleteCases[gdps, _Missing];沒有輸出——這個步驟是在為下一步做準備。
{Length[filteredgdps], Max[filteredgdps]}{1, CountryData[CountryData[All, GDP]]}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}};沒有輸出——這個步驟是在為下一步做準備。
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]baddata[entry_] := Not[MatchQ[entry, {1 | 2, _ ? NumberQ, _ ? NumberQ}]]沒有輸出——這個步驟是在為下一步做準備。
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}}badgroup[entry_] := Not[MatchQ[entry[[1]], 1 | 2]]沒有輸出——這個步驟是在為下一步做準備。
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}}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}}group1mean = Mean[Select[group1[[All, -1]], NumberQ]]2.104filtereddata[[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}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]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]]]]沒有輸出——這個步驟是在為下一步做準備。
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}}replaceByGroup[newdata, 3, 1, Mean]{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}replaceByGroup[newdata, 3, 1, Median]{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 1.84, 1.84}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}}]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 用戶端使用這個