【问题标题】:R Subtracting Vectors Time SeriesR 减去向量时间序列
【发布时间】:2014-04-19 12:09:00
【问题描述】:

尽管有卷积,这段代码仍然有效,所以我可以接受下面的代码。

从 CSV 文件到数据帧,再到列表再到数据帧,然后到 xts/zoo。

有没有更简单和更短的方法来减去两个向量?

library(xts)
#Step 1  CSV
dat<-read.csv("table(3).csv")
#first row in that file
#         Date  Open  High   Low Close   Volume Adj.Close
#1  2014-04-17 21.85 21.91 21.75 21.86 47892800     21.86
#
class(dat)
#[1] "data.frame"

# Step 2 
h<-subset(dat,select=c(High))
l<-subset(dat,select=c(Low))
d<-subset(dat,select=c(Date))
diff<-h-l

#Step 3
new<-c(d,h,l,diff)
class(new)
#[1] "list"

new2<-as.data.frame(new)
class(new2)
#[1] "data.frame"

#Step 4
new2.xts <- xts(x=new2[,-1],order.by= as.POSIXct(new2$Date))
class(new2.xts)
#[1] "xts" "zoo"

【问题讨论】:

    标签: r vector xts subtraction


    【解决方案1】:

    您可以直接与列交互:

    dat$diff <- dat$High - dat$Low
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多