0

这在 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)

我收到此错误:

as.Date.POSIXlt(NextH2OMonth) 中的错误:非空 POSIXlt 结构中的零长度分量

任何想法为什么?我需要系统地添加一年(对于一个循环)和一个月(对于另一个循环),并将生成的更改变量与 Date 类的值进行比较,这就是使用 as.Date() 将它们转换回的原因。

谢谢,汤姆

编辑:

下面是整个代码部分。我正在使用 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 中也会出现同样的错误。当逐行执行时,它不是问题,当它作为脚本运行时,它就是它。

4

3 回答 3

1

纯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 对诸如mondate("2013-01-31")+ 1给出 2 月的最后一天而seq(as.Date("2013-01-31"), length = 2, by = "month")[2]给出 3 月 3 日之类的事情要聪明一些。

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

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

添加评论POSIXlt和部分yearmon

于 2013-02-13T06:06:18.633 回答
0

这是您可以使用 package 将 1 个月添加到 R 中的日期lubridate

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 日不存在)。

于 2013-02-13T03:51:29.453 回答
0

您能否提供一个可重现的示例?里面有什么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"
于 2013-02-13T06:56:37.777 回答