在R中将日期列表转换为数据帧

在R中将日期列表转换为数据帧,r,dataframe,date,R,Dataframe,Date,这是我在StackOverflow上找到的一个函数,它可以让一个人创建一年中的交易日期。我无法将最终结果更改为日期数据框。有人能教我怎么做吗?请帮忙。非常感谢。我打电话: TradingDates(2010:2011) 代码如下: #' Get Trading Dates for one or more years #' #' Get a vector of dates of non-holiday weekdays. #' #' This uses holiday calendar fu

这是我在StackOverflow上找到的一个函数,它可以让一个人创建一年中的交易日期。我无法将最终结果更改为日期数据框。有人能教我怎么做吗?请帮忙。非常感谢。我打电话:

 TradingDates(2010:2011)
代码如下:

#' Get Trading Dates for one or more years
#'
#' Get a vector of dates of non-holiday weekdays.
#' 
#' This uses holiday calendar functions (\code{holidayNYSE} by default)
#' from the \emph{timeDate} package.  If \emph{timeDate} is not loaded, it 
#' will be temporarily loaded, then unloaded \code{on.exit}.
#' 
#' @param year vector of 4 digit years or something that is coercible to 
#'   a vector 4 digit years via \code{as.numeric}
#' @param FUN a function that takes a \code{year} argument and returns a vector
#'   that can be coerced to a \code{Date}.  \code{holidayNYSE} by default. Most
#'   likely, this will be one of:  \sQuote{holidayLONDON}, \sQuote{holidayNERC}, 
#'   \sQuote{holidayNYSE}, \sQuote{holidayTSX}, \sQuote{holidayZURICH}
#' @return a vector of all dates in \code{years} that are weekdays and not 
#'   holidays.
#' @author GSee
#' @examples
#' \dontrun{
#' TradingDates(2012)
#' TradingDates(2010:2011)
#' }
#' @export
TradingDates <- function(year=format(Sys.Date(), "%Y"), FUN=holidayNYSE) {
  # the next few lines should be removed when this code is added to a package
  # that Imports timeDate
  if (!"package:timeDate" %in% search()) {
    suppressPackageStartupMessages({ 
      if (!require(timeDate)) {
        stop("timeDate must be installed to use this function.")
      }
    })
    on.exit(detach(package:timeDate, unload=TRUE))
  }
  ## End of code that should be removed when this is added to a package
  year <- as.numeric(year)
  fun <- match.fun(FUN)
  do.call('c', lapply(year, function(y) {
    holidays <- as.Date(fun(year=y))
    all.days <- seq.Date(as.Date(paste(y, '01-01', sep='-')), 
                         as.Date(paste(y, '12-31', sep='-')), by='days')
    nohol <- all.days[!all.days %in% holidays]
    nohol[!format(nohol, '%w') %in% c("6", "0")] #neither holiday nor weekend
  }))
}


#' Get Date of previous (next) trading day
#' 
#' Get the Date of the previous (next) trading day.
#' 
#' For \code{PrevTradingDate}, \code{n} is the number of days to go back. So,
#' if \code{n} is 2 and today is a a Monday, it would return the date of the
#' prior Thursday because that would be 2 trading days ago.
#' \code{n} works analogously in \code{NextTradingDate}.
#'
#' The maximum value that \code{n} can be is the total number of days in the
#' year prior to \code{Date} plus the total number of years in the current 
#' year of \code{Date}.  So, on the last day of the year, the max value of
#' \code{n} will usually be \code{504} (because most years have 252 trading 
#' days).  One the first day of the year, the max value of \code{n} will usually
#' be \code{252}.
#'
#' @param n number of days to go back. 1 is the previous trading day; 2 is the
#'   trading day before that, etc.  \code{n} should be less than 365, but see
#'   details
#' @param Date a \code{Date} or something that can be coerced to a \code{Date}
#' @return \code{PrevTradingDate} returns the date of the previous trading day 
#' up to, but not including, \code{Date}.  \code{NextTradingDate} returns the 
#' date of the next trading day.
#' @author GSee
#' @seealso \code{\link{TradingDates}}
#' @examples
#' \dontrun{
#' PrevTradingDate()
#' PrevTradingDate('2012-01-03')
#' NextTradingDate()
#' NextTradingDate('2012-12-24')
#' }
#' @export
#' @rdname PrevTradingDate
PrevTradingDate <- function(Date=Sys.Date(), n=1) {
  stopifnot(require(xts)) #remove this line when this is added to a package that Imports xts (needed for first/last)
  D <- as.Date(Date)
  y <- as.numeric(format(D, "%Y"))
  trading.days <- TradingDates(y)
  out <- trading.days[trading.days < Date]
  if (length(out) >= n) {
    first(last(out, n))
  } else { 
    prev.year.td <- TradingDates(y - 1)
    max.n <- length(out) + length(prev.year.td)
    if (n > max.n) stop("'n' is too large. Try something less than 252.")
    new.n <- n - length(out) # we need this many trading days from previous year
    # if it's the 1st trading day of the year, return the last trading date of
    # previous year
    first(last(TradingDates(y - 1), new.n))
  } 
}

#' @export
#' @rdname PrevTradingDate
NextTradingDate <- function(Date=Sys.Date(), n=1) {
  stopifnot(require(xts)) #remove this line when this is added to a package that Imports xts (needed for first/last)
  D <- as.Date(Date)
  y <- as.numeric(format(D, "%Y"))
  trading.days <- TradingDates(y)
  out <- trading.days[trading.days > Date]
  if (length(out) >= n) {
    last(first(out, n))
  } else { 
    next.year.td <- TradingDates(y + 1)
    max.n <- length(out) + length(next.year.td)
    new.n <- n - length(out) # how many trading days we need from next year
    if (n > max.n) stop("'n' is too large. Try something less than 252.")
    # if it's the last trading day of the year, return the first trading date of
    # next year
    last(first(TradingDates(y + 1), new.n))
  }
}
#“获取一年或几年的交易日期
#'
#'获取非假日工作日的日期向量。
#' 
#'这使用假日日历函数(\code{holidayNYSE}默认)
#'来自\emph{timeDate}包。如果\emph{timeDate}未加载,则
#'将临时加载,然后卸载\code{on.exit}。
#' 
#“@param year向量为4位年份或可强制为
#'通过\code{as.numeric}的4位年份向量
#“@param FUN接受\code{year}参数并返回向量的函数
#'可以强制为\code{Date}\默认情况下,代码为{holidayNYSE}。最
#'很可能,这将是其中之一:\sQuote{holidayLONDON}、\sQuote{holidayNERC},
#'\sQuote{holidayNYSE}、\sQuote{holidayTSX}、\sQuote{holidayZURICH}
#“@返回\code{years}中的所有日期的向量,这些日期是工作日而不是
#“假期。
#“@作者GSee
#“@示例
#“\dontrun{
#“交易日期(2012年)
#交易日期(2010:2011)
#' }
#“@出口

TradingDates函数的输出是
日期的
向量。因此,我们可以使用
data.frame

df1 <-  data.frame(date = TradingDates(2010:2011))
str(df1)
#'data.frame':  504 obs. of  1 variable:
#$ date: Date, format: "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" ...

df1@KevinK我在复制/粘贴你的函数。它也应该在Rstudio中起作用。你能试穿一件新的吗session@KevinK我查了一下Rstudio。这对我来说很管用,工作起来很有魅力。谢谢大家!@KevinK您可以使用
df1@akurn非常感谢!非常感谢。