Last active
June 4, 2018 15:22
-
-
Save sjednac/1561e903678e5a9296a7d0667110e649 to your computer and use it in GitHub Desktop.
Fill missing value with the latest known element
This file contains hidden or 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
import Double.NaN | |
object FillMissingValues1 extends App { | |
val data = List(1.0, NaN, 2.0, NaN, NaN, 3.0, 4.0, NaN, 5.0, NaN) | |
val res = data.scanLeft(0.0)( (current, item) => if (item.isNaN) current else item) | |
println(res) | |
} |
This file contains hidden or 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
import Double.NaN | |
final case class Measurement(values: Seq[Double]) | |
object FillMissingValues2 extends App { | |
val data = Seq( | |
Measurement( | |
Seq(0.0, NaN, 2.0, 3.0, NaN, 5.0, NaN, 7.0, NaN, NaN) | |
), | |
Measurement( | |
Seq(100.0, 101.0, NaN, 103.0, 104.0, NaN, NaN, 107.0, 108.0, 109.0) | |
), | |
Measurement( | |
Seq(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN) | |
) | |
) | |
val zero = Measurement(values = (1 to 10).map(_ => 0.0)) | |
val res = data.scanLeft(zero)( (current, item) => { | |
if (item.values.exists(v => v.isNaN)) { | |
item.copy(values = item.values.zip(current.values).map{ case (v, current) => if (v.isNaN) current else v }) | |
} else { | |
item | |
} | |
}).drop(1) | |
println(res.map(m => m.values.mkString(",")).mkString("\n")) | |
} |
This file contains hidden or 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
# FillMissingValues1 | |
List(0.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 4.0, 4.0, 5.0, 5.0) |
This file contains hidden or 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
# FillMissingValues2 | |
0.0,0.0,2.0,3.0,0.0,5.0,0.0,7.0,0.0,0.0 | |
100.0,101.0,2.0,103.0,104.0,5.0,0.0,107.0,108.0,109.0 | |
100.0,101.0,2.0,103.0,104.0,5.0,0.0,107.0,108.0,109.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment