使用合并函数

时间:2015-05-13 22:18:23

标签: r merge xts quantmod

您好我正在使用R quantmod库,我想查找并返回最多两个值(今天的数量,昨天的数量)。

require(quantmod)
getSymbols("HELE")
# Ok now when I do this it does not return a single column with the highest 
# volume 
head(    merge( HELE, max (HELE$HELE.Volume,lag(HELE$HELE.Volume, k=1    )  ) ) )

这通常有效,因为例如说我想从昨天的收盘价中减去今天的高点我可以这样做。

head(    merge(HELE, abs(HELE$HELE.High - lag(HELE$HELE.Close, k=1)   ) ) )

我也尝试过应用功能,但效果不好,

head(    merge(HELE, as.xts(apply( c(lag(HELE$HELE.Volume, k=1    ), HELE$HELE.Volume    ), 1, max) )      ) )

提前致谢。 Ahdee

1 个答案:

答案 0 :(得分:3)

试试这个:

[
    {
    "id": null,
    "text": "Root",
    "icon": "tree.png",
    "state": null,
    "children": [
        {
        "id": null,
        "text": "folder1",
        "children": [
        {
        "id": null,
        "text": "test",
            "children": [
             {
                "id": null,
                "text": "image1.jpg",
                "icon": "glyphicon glyphicon-picture",
                "children": []
             }
            ]
         }
      ]
  },
 {
    "id": null,
    "text": "folder1",
    "children": [
      {
        "id": null,
        "text": "pruebas",
        "children": [
          {
            "id": null,
            "text": "image2.jpg",
            "icon": "glyphicon glyphicon-picture",
            "children": []
          }
        ]
      }
    ]
  },
  {
    "id": null,
    "text": "folder1",
    "children": [
      {
        "id": null,
        "text": "test",
        "children": [
          {
            "id": null,
            "text": "image3.jpg",
            "icon": "glyphicon glyphicon-picture",
            "children": []
          }
        ]
      }
    ]
  }
]
}
]

head(merge(HELE, pmax (HELE$HELE.Volume,lag(HELE$HELE.Volume, k=1), na.rm=TRUE))) pmax的矢量化版本,即它在两个向量之间找到成对max。您还需要包含max,否则您将获得缺少值的NA。

仅使用na.rm=TRUE,它会在两个向量之间找到全局最大值,并创建一个仅填充此值的列。

输出:

max