简体   繁体   English

R xts如何添加计算列

[英]R xts how to add calculated column

I have two series that I have joined in a single xts object using merge(foo, footoo, all = FALSE) . 我有两个系列,我使用merge(foo, footoo, all = FALSE)在一个xts对象中加入。

Now I need to take a new vector = foo/footoo : 现在我需要一个新的vector = foo / footoo:

  1. either as a new column in the existing xts object 或者作为现有xts对象中的新列
  2. or as a new xts object with the same index. 或者作为具有相同索引的新xts对象。

I'm trying to avoid use of cbind because I can't bring myself to haphazardly join an unindexed vector of data to my safely ordered xts object. 我试图避免使用cbind因为我无法随意将未经索引的数据向量加入到我安全排序的xts对象中。

Do I need to coerce to something like data.frame (for which I would know how to do this)? 我是否需要强制使用data.frame(我知道如何做到这一点)? But if so, how do I keep my index intact? 但如果是这样,我如何保持我的索引完整? It's the ordering that has me nervous. 这是令我紧张的顺序

I'm very new to R and this is the first time I've worked with time series in R, so I apologize if this question has an answer that is obvious to all but me. 我是R的新手,这是我第一次使用R中的时间序列,所以如果这个问题的答案对我来说很明显,我会道歉。

Thanks in advance. 提前致谢。

Using transform for example, you can create a new column like this: 例如,使用transform ,您可以创建一个新列,如下所示:

obj <- merge(foo, footoo, all = FALSE)  
transform(obj, newfoo = foo/footoo )

You can safely do as below. 您可以安全地执行以下操作。 xts will always cbind or merge by time index. xts将始终按时间索引进行cbindmerge

mergedXTS <- merge(foo, footoo, all=FALSE)
mergedXTS$newfoo <- mergedXTS$foo/mergedXTS$footoo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM