Qurak

Replace or remove invalid or missing data

How-to 23 stappen, op volgorde uitgevoerd in één sessie. Elke stap is tegen de engine uitgevoerd en de uitvoer hieronder is wat hij opleverde.

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

Geen uitvoer - deze stap zet iets klaar voor de volgende.

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

Geen uitvoer - deze stap zet iets klaar voor de volgende.

Stap 7
{Length[filteredgdps], Max[filteredgdps]}
Uitvoer
{1, CountryData[CountryData[All, GDP]]}
Stap 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}};

Geen uitvoer - deze stap zet iets klaar voor de volgende.

Stap 9
Grid[data, Dividers -> All]
Uitvoer
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]
Stap 10
baddata[entry_] := Not[MatchQ[entry, {1 | 2, _ ? NumberQ, _ ? NumberQ}]]

Geen uitvoer - deze stap zet iets klaar voor de volgende.

Stap 11
DeleteCases[data, _ ? baddata]
Uitvoer
{{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}}
Stap 12
badgroup[entry_] := Not[MatchQ[entry[[1]], 1 | 2]]

Geen uitvoer - deze stap zet iets klaar voor de volgende.

Stap 13
filtereddata = DeleteCases[data, _ ? badgroup]
Uitvoer
{{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}}
Stap 14
group1 = Select[filtereddata, #[[1]] === 1&]
Uitvoer
{{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}}
Stap 15
group1mean = Mean[Select[group1[[All, -1]], NumberQ]]
Uitvoer
2.104
Stap 16
filtereddata[[All, 3]] = filtereddata[[All, 3]] /. "NA" -> group1mean
Uitvoer
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}
Stap 17
Grid[filtereddata, Dividers -> All]
Uitvoer
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]
Stap 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]]]]

Geen uitvoer - deze stap zet iets klaar voor de volgende.

Stap 19
newdata = DeleteCases[data, _ ? badgroup]
Uitvoer
{{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}}
Stap 20
replaceByGroup[newdata, 3, 1, Mean]
Uitvoer
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}
Stap 21
replaceByGroup[newdata, 3, 1, Median]
Uitvoer
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 1.84, 1.84}
Stap 22
Grid[Transpose[Table[If[j == 1, newdata[[All, j]], replaceByGroup[newdata, j, 1, Mean]], {j, Length[newdata[[1]]]}]]]
Uitvoer
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}}]
Stap 23
Grid[Transpose[{newdata[[All, 1]], replaceByGroup[newdata, 2, 1, Mean], replaceByGroup[newdata, 3, 1, Median]}]]
Uitvoer
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}}]

Gebruikte functies

Gerelateerde recepten

Alle recepten · Functiereferentie · Gebruik dit vanuit een MCP-client