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