Created
March 9, 2017 04:45
-
-
Save x7c1/f2bedd66b594bd19b326ea43e3ef46f5 to your computer and use it in GitHub Desktop.
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
| object Sample { | |
| def main(args: Array[String]): Unit = { | |
| val x = Seq( | |
| Seq("F", "F", "M", "M", "M") map ("SEX" -> _), | |
| Seq(158, 162, 177, 173, 166) map ("HEIGHT" -> _), | |
| Seq(51D, 55D, 72D, 57D, 64D) map ("WEIGHT" -> _) | |
| ).transpose | |
| val heights = x flatMap (_ collect { case ("HEIGHT", n) => n }) | |
| println(heights) // List(158, 162, 177, 173, 166) | |
| val mean = { | |
| val weights = x flatMap (_ collect { case ("WEIGHT", n: Double) => n }) | |
| weights.sum / weights.length | |
| } | |
| println(mean) // 59.8 | |
| val seconds = x collect { case _ :+ ((_, n: Int)) :+ _ => n } | |
| println(seconds) // List(158, 162, 177, 173, 166) | |
| val line0 = x(0) | |
| println(line0) // List((SEX,F), (HEIGHT,158), (WEIGHT,51.0)) | |
| val height0 = line0 collectFirst { case ("HEIGHT", n) => n } | |
| println(height0) // Some(158) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rf. http://cse.naro.affrc.go.jp/takezawa/r-tips/r/39.html