Created
September 29, 2014 19:33
-
-
Save mormegil-cz/12006a0f785a254a86cc to your computer and use it in GitHub Desktop.
Triviální statistické zpracování volebních dat z projektu Otevřené volby (ovolby.cz); v tomto případě zjištění rekordně nízkých účastí
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LINQPad C# Program | |
const int COL_VOLBY = 0; | |
const int COL_OBVOD = 1; | |
const int COL_OBEC = 3; | |
const int COL_K1_VOLICI = 6; | |
const int COL_K1_OBALKY = 7; | |
const int COL_K2_VOLICI = 10; | |
const int COL_K2_OBALKY = 11; | |
void Main() | |
{ | |
// http://ovolby.cz/election.php?kind=senat | |
var data = LoadCsv(@"senat_1996-2013_okrsky.csv").Skip(1) | |
.Select(row => new { | |
Volby = row[COL_VOLBY], | |
Obvod = row[COL_OBVOD], | |
Obec = row[COL_OBEC], | |
Volici1 = Int(row[COL_K1_VOLICI]), | |
Obalky1 = Int(row[COL_K1_OBALKY]), | |
Volici2 = Int(row[COL_K2_VOLICI]), | |
Obalky2 = Int(row[COL_K2_OBALKY]), | |
}) | |
.ToList(); | |
var results = data | |
.GroupBy(row => new { row.Volby, row.Obvod }) | |
.Select(g => new { g.Key, Volici1 = g.Sum(r => r.Volici1), Obalky1 = g.Sum(r => r.Obalky1), Volici2 = g.Sum(r => r.Volici2), Obalky2 = g.Sum(r => r.Obalky2) }) | |
.Select(g => new { | |
g.Key, | |
g.Obalky1, g.Volici1, | |
g.Obalky2, g.Volici2, | |
Ucast1 = Percent(g.Obalky1, g.Volici1), | |
Ucast2 = Percent(g.Obalky2, g.Volici2) | |
}) | |
.ToList(); | |
results.OrderBy(g => g.Ucast1).Take(5).Dump(); | |
results.OrderBy(g => g.Ucast2).Take(5).Dump(); | |
} | |
IEnumerable<string[]> LoadCsv(string filename) | |
{ | |
using (var reader = new StreamReader(filename, Encoding.UTF8)) | |
{ | |
string line; | |
var rowList = new List<string>(); | |
var sb = new StringBuilder(); | |
while ((line = reader.ReadLine()) != null) | |
{ | |
var inQuotes = false; | |
foreach(var c in line) | |
{ | |
if (inQuotes) | |
{ | |
if (c == '"') | |
{ | |
inQuotes = false; | |
// TODO: Double-quote escaping | |
} | |
else | |
{ | |
sb.Append(c); | |
} | |
} | |
else | |
{ | |
if (c == '"') | |
{ | |
inQuotes = true; | |
} | |
else if (c == ',') | |
{ | |
rowList.Add(sb.ToString()); | |
sb.Clear(); | |
} | |
else | |
{ | |
sb.Append(c); | |
} | |
} | |
} | |
if (inQuotes) throw new NotSupportedException("Quoted linebreaks not supported"); | |
rowList.Add(sb.ToString()); | |
sb.Clear(); | |
var result = rowList.ToArray(); | |
rowList.Clear(); | |
yield return result; | |
} | |
} | |
} | |
int Int(string str) | |
{ | |
if (String.IsNullOrEmpty(str)) return 0; | |
int result; | |
if (!Int32.TryParse(str, NumberStyles.None, CultureInfo.InvariantCulture, out result)) | |
throw new FormatException("Invalid number: " + str); | |
return result; | |
} | |
double Percent(int x, int y) | |
{ | |
return y == 0 ? 100.0 : (100.0 * x / y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment