Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r中x个月前数据的子集_R - Fatal编程技术网

r中x个月前数据的子集

r中x个月前数据的子集,r,R,我有一个来自雅虎财经的数据,我想找到3个月前的数据子集,然后在6个月前也这样做。我的DF是: getSymbols("^IRX", from="2019-01-02") 最简单的解决方案是从今天起使用-90和-180之类的子集 Df.3m <- IRX[paste(Sys.Date()-90,"::", sep="")] Df.6m <- IRX[paste(Sys.Date()-180,"::", sep="")] Df.3m我最终创建了一个似乎有效的快速函数: Find.Da

我有一个来自雅虎财经的数据,我想找到3个月前的数据子集,然后在6个月前也这样做。我的DF是:

getSymbols("^IRX", from="2019-01-02")
最简单的解决方案是从今天起使用-90和-180之类的子集

Df.3m <- IRX[paste(Sys.Date()-90,"::", sep="")]
Df.6m <- IRX[paste(Sys.Date()-180,"::", sep="")]

Df.3m我最终创建了一个似乎有效的快速函数:

Find.Date.x.month.ago <- function(current_date, x){
  Current.month <- month(current_date)
  Past.x.month <- Current.month - x
  if(Past.x.month < 0){
    Past.x.month <- 12 + Past.x.month  
  }
    trgt.day <- day(current_date)
  while (is.na(lubridate::ymd(paste(year(current_date), Past.x.month, trgt.day, sep="-"))) == TRUE) {
    trgt.day <- trgt.day - 1
  }
  return(lubridate::ymd(paste(year(current_date), Past.x.month, trgt.day, sep="-")))
}

Find.Date.x.month.ago我最终创建了一个似乎有效的快速函数:

Find.Date.x.month.ago <- function(current_date, x){
  Current.month <- month(current_date)
  Past.x.month <- Current.month - x
  if(Past.x.month < 0){
    Past.x.month <- 12 + Past.x.month  
  }
    trgt.day <- day(current_date)
  while (is.na(lubridate::ymd(paste(year(current_date), Past.x.month, trgt.day, sep="-"))) == TRUE) {
    trgt.day <- trgt.day - 1
  }
  return(lubridate::ymd(paste(year(current_date), Past.x.month, trgt.day, sep="-")))
}

Find.Date.x.month.ago您可以使用lubridate包获取抵销日期:

library(lubridate)
dt <- Sys.Date() %m-% months(3)
Df.3m <- IRX[paste0(dt,"::")]
库(lubridate)

dt您可以使用lubridate软件包获取抵销日期:

library(lubridate)
dt <- Sys.Date() %m-% months(3)
Df.3m <- IRX[paste0(dt,"::")]
库(lubridate)

dt月是一个糟糕的分析单位,因为它们不一致。这导致了各种各样的问题,尤其是在二月。根据你所做的,你应该考虑一个30天的间隔,或者是每两周一次。每只股票的价格会有所不同吗?@Reeza,我同意这很棘手,我想动态计算三个月的平均价格和三个月的stdev。这三个月对所有适用的股票来说都是一样的,因为它们都在同一个市场/区域。因为它们不一致,所以每个月都是一个糟糕的分析单位。这导致了各种各样的问题,尤其是在二月。根据你所做的,你应该考虑一个30天的间隔,或者是每两周一次。每只股票的价格会有所不同吗?@Reeza,我同意这很棘手,我想动态计算三个月的平均价格和三个月的stdev。这三个月对所有适用的股票都是一样的,因为它们都在同一个市场/区域