dgadiraju
5/24/2017 - 9:14 AM

scala-notabystate.scala

import scala.io.Source

val fileName = "/Users/itversity/Research/data/elections/ls2014.tsv"
val results = Source.fromFile(fileName).getLines

val notas = results.filter(rec => rec.split("\t")(2) == "None of the Above")
// val notas = results.partition(rec => rec.split("\t")(2) == "None of the Above")._1
val notaByState = notas.
  map(rec => (rec.split("\t")(0), rec.split("\t")(10).toInt)).
  toList.
  groupBy(rec => rec._1).
  map(rec => (rec._1, rec._2.map(_._2).reduce((a, b) => a + b))).
  toList.
  sortBy(rec => -rec._2)

notaByState.foreach(rec => println(rec._1 + "\t" + rec._2))