在R中使用POSIXlt向X添加一个月的问题-需要使用as.Date(X)重置值



这在R:中对我有效

# Setting up the first inner while-loop controller, the start of the next water year
  NextH2OYear <- as.POSIXlt(firstDate)
  NextH2OYear$year <- NextH2OYear$year + 1
  NextH2OYear<-as.Date(NextH2OYear)

但事实并非如此:

# Setting up the first inner while-loop controller, the start of the next water month
  NextH2OMonth <- as.POSIXlt(firstDate)
  NextH2OMonth$mon <- NextH2OMonth$mon + 1
  NextH2OMonth <- as.Date(NextH2OMonth)

我得到这个错误:

错误为.Date.POSIXlt(下一个月):非空POSIXlt结构中的零长度分量

有什么想法吗?我需要系统地添加一年(对于一个循环)和一个月(对于另一个循环

谢谢,Tom

编辑:

下面是整个代码部分。我使用的是RStudio(版本0.97.306)。下面的代码表示一个函数,该函数传递一个由两列组成的数组(日期(CLass=Date)和出院数据(CLass=Numeric)),用于计算月平均值。因此,firstDate和lastDate是Date类,由传递的数组确定。这段代码改编自计算年平均值的成功代码——可能还有一两件事我还需要更改,但由于我在使用POSIXlt时遇到的早期错误,我无法对以后的部分进行错误检查。这是代码:

MonthlyAvgDischarge<-function(values){  
  #determining the number of values - i.e. the number of rows
  dataCount <- nrow(values)
  # Determining first and last dates
  firstDate <- (values[1,1])
  lastDate <- (values[dataCount,1])
  # Setting up vectors for results
  WaterMonths <- numeric(0)
  class(WaterMonths) <- "Date"
  numDays <- numeric(0)
  MonthlyAvg <- numeric(0)
  # while loop variables
  loopDate1 <- firstDate
  loopDate2 <- firstDate
  # Setting up the first inner while-loop controller, the start of the next water month
  NextH2OMonth <- as.POSIXlt(firstDate)
  NextH2OMonth$mon <- NextH2OMonth$mon + 1
  NextH2OMonth <- as.Date(NextH2OMonth)
  # Variables used in the loops
  dayCounter <- 0
  dischargeTotal <- 0
  dischargeCounter <- 1
  resultsCounter <- 1
  loopCounter <- 0
  skipcount <- 0
  # Outer while-loop, controls the progression from one year to another
  while(loopDate1 <= lastDate)
  {
    # Inner while-loop controls adding up the discharge for each water year
    # and keeps track of day count
    while(loopDate2 < NextH2OMonth)
    {
      if(is.na(values[resultsCounter,2]))
      {
        # Skip this date
        loopDate2  <- loopDate2 + 1
        # Skip this value
        resultsCounter <- resultsCounter + 1
        #Skipped counter
        skipcount<-skipcount+1
      } else{
        # Adding up discharge
        dischargeTotal <- dischargeTotal + values[resultsCounter,2]
      }
      # Adding a day
      loopDate2  <- loopDate2 + 1
      #Keeping track of days
      dayCounter <- dayCounter + 1
      # Keeping track of Dicharge position
      resultsCounter <- resultsCounter + 1
    }
    # Adding the results/water years/number of days into the vectors
    WaterMonths <- c(WaterMonths, as.Date(loopDate2, format="%mm/%Y"))
    numDays <- c(numDays, dayCounter)
    MonthlyAvg <- c(MonthlyAvg, round((dischargeTotal/dayCounter), digits=0))
    # Resetting the left hand side variables of the while-loops 
    loopDate1 <- NextH2OMonth
    loopDate2 <- NextH2OMonth
    # Resetting the right hand side variable of the inner while-loop
    # moving it one year forward in time to the next water year
    NextH2OMonth <- as.POSIXlt(NextH2OMonth)
    NextH2OMonth$year <- NextH2OMonth$Month + 1
    NextH2OMonth<-as.Date(NextH2OMonth)
    # Resettting vraiables that need to be reset
    dayCounter <- 0 
    dischargeTotal <- 0
    loopCounter <- loopCounter + 1
  } 
  WaterMonths <- format(WaterMonthss, format="%mm/%Y")
  # Uncomment the line below and return AvgAnnualDailyAvg if you want the water years also 
  # AvgAnnDailyAvg <- data.frame(WaterYears, numDays, YearlyDailyAvg) 
  return((MonthlyAvg))
}  

在常规R中也会出现同样的错误。当逐行执行时,这不是问题,当将其作为脚本运行时,它就是问题。

普通R

seq(Sys.Date(), length = 2, by = "month")[2]
seq(Sys.Date(), length = 2, by = "year")[2]

请注意,这也适用于POSIXlt,例如

seq(as.POSIXlt(Sys.Date()), length = 2, by = "month")[2]

星期一

library(mondate)
now <- mondate(Sys.Date())
now + 1  # date in one month
now + 12  # date in 12 months

周一对mondate("2013-01-31")+ 1表示二月的最后一天,而seq(as.Date("2013-01-31"), length = 2, by = "month")[2]表示三月三日这样的事情更聪明。

yearmon如果你真的不需要白天的部分,那么yearmon可能更可取:

library(zoo)
now.ym <- yearmon(Sys.Date())
now.ym + 1/12 # add one month
now.ym + 1 # add one year

增加了对POSIXlt的评论和对yearmon的部分。

以下是您可以使用包lubridate:在R中的日期上添加1个月

library(lubridate)
x <- as.POSIXlt("2010-01-31 01:00:00")
month(x) <- month(x) + 1
>x
[1] "2010-03-03 01:00:00 PST"

(请注意,它正确处理了添加,因为2月31日不存在)。

您能否提供一个可复制的示例?firstDate中有什么,您使用的R是什么版本?我经常对POSIXlt日期进行这种操作,它似乎很有效:

Sys.Date()
# [1] "2013-02-13"
date = as.POSIXlt(Sys.Date())
date$mon = date$mon + 1
as.Date(date)
# [1] "2013-03-13"

相关内容

  • 没有找到相关文章

最新更新