Problemy z dodaniem miesiąca do X za pomocą POSIXlt w R - należy zresetować wartość za pomocą as.Date (X)
To działa dla mnie w 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)
Ale to nie:
# 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)
Dostaję ten błąd:
Błąd w as.Date.POSIXlt (NextH2OMonth): komponent o zerowej długości w niepustej strukturze POSIXlt
Jakieś pomysły dlaczego? Muszę systematycznie dodawać jeden rok (dla jednej pętli) i jeden miesiąc (dla innej pętli) i porównywać wynikowe zmienione zmienne z wartościami z klasą Date, dlatego są one konwertowane z powrotem za pomocą as.Date ().
Dzięki, Tom
Edytować:
Poniżej znajduje się cała sekcja kodu. Używam RStudio (wersja 0.97.306). Poniższy kod reprezentuje funkcję przekazywaną przez tablicę dwóch kolumn (Data (CLass = Date) i Discharge Data (Class = Numeric), które są używane do obliczania średnich miesięcznych. Tak więc firstDate i lastDate są datą klasy i są określane na podstawie ten kod jest dostosowany z udanego kodu, który oblicza średnie roczne - może być jedna lub dwie rzeczy, które muszę jeszcze zmienić, ale nie mogę sprawdzić błędów późniejszych części z powodu wczesnych błędów, które otrzymuję podczas korzystania z POSIXlt Oto kod:
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))
}
Ten sam błąd występuje w zwykłym R. Gdy robisz to po linii, to nie jest problem, gdy uruchamiasz go jako skrypt, to jest to.