1

我正在使用 Scala 鞍座框架系列

鞍系列可以转换为框架,鞍框架可以使用 concat 方法连接,如下所示:

val frame3 = frame1.concat(frame2)

由于我是 Scala 新手,因此我一直在尝试转换

Map[String, Series[String, Double] 

Frame[String, String, Double] 

其中 Frame 列索引是 Map 键,列是 Map 值。

有人可以提出解决方案吗?

4

2 回答 2

1

你可能已经解决了这个问题。如果没有,这里有一个似乎适合的解决方案:

object TheApp extends App {

  import org.saddle.{Frame, Index, Series, Vec}

  val inMap = Map(
    "s1" -> Series(Vec(1.0, 2.0, 3.0, 4.0), Index("a", "b", "b", "c")),
    "s2" -> Series(Vec(2.0, 3.0, 4.0, 5.0), Index("A", "B", "B", "C")),
    "s3" -> Series(Vec(3.0, 4.0, 5.0, 6.8), Index("t", "C", "v", "v"))
  )

  val frame = inMap.foldLeft(Frame.empty[String,String,Double]) { (prevFrame, mapElement) =>
    val (key, series) = mapElement
    val f = Frame(key -> series)
    prevFrame.concat(f)
  }

  println(s"Saddle Test map=$inMap frame=$frame")
}

这里发生的事情是foldLeft将每个地图元素与先前计算的结果一起使用,并调用提供的函数来计算下一个值。

感谢您让我了解 Saddle,我可能会发现它非常有用!

于 2013-11-30T18:34:02.130 回答
1

为了将来参考,当索引在它们之间是共同的时,这是一种并排连接向量的简单方法:

import org.saddle.{Frame, Index, Series, Vec}

val ix = Index("2015-03-19", "2015-03-20", "2015-03-21", "2015-03-22", "2015-03-23")
val s1 = Series( Vec(1.0,        2.0,        3.0, 4.0, 5.0), ix)
val s2 = Series( Vec(Double.NaN, 1.0,        2.0, 3.0, 4.0), ix)
val s3 = Series( Vec(Double.NaN, Double.NaN, 1.0, 2.0, 3.0), ix)

val map = Map( "RSI" -> s1, "ADX" -> s2, "SAR" -> s3)

val frame =
  map
  .foldLeft(Frame.empty[String,String,Double]) {
    case (prevFrame, mapElement) =>
      val (key, series) = mapElement
      val f = Frame(key -> series)
      prevFrame.rconcat(f) }

输出是:

[5 x 3]
                 RSI    ADX    SAR 
              ------ ------ ------ 
2015-03-19 -> 1.0000     NA     NA 
2015-03-20 -> 2.0000 1.0000     NA 
2015-03-21 -> 3.0000 2.0000 1.0000 
2015-03-22 -> 4.0000 3.0000 2.0000 
2015-03-23 -> 5.0000 4.0000 3.0000 

请注意,正在使用rconcat 。

于 2015-04-01T22:30:26.823 回答