使用Rstudio中hydroTSM包的dm2seasonal函数计算逐日时间序列季节观测值时遇错求助
dm2seasonal Error for Cross-Year Winter Calculations Hey there! Let's work through this issue with the dm2seasonal function from the hydroTSM package—cross-year seasons like winter (spanning December, January, February) are a common pain point, so you're not alone here. That error Error in 1:(nrow(s.a) - 1) : argument of length 0 means the function is trying to work with an empty intermediate data frame (s.a), which usually happens when it can't properly identify or process the winter time window. Here are the most likely fixes and alternatives:
1. Ensure Your Data is in a Valid Time Series Format
hydroTSM functions rely heavily on properly formatted time series objects (like xts or zoo) instead of plain data frames. If your date column isn't parsed correctly, the function can't recognize the cross-year winter interval.
Try this step-by-step conversion:
library(hydroTSM) library(xts) # First, parse your date column correctly (note your format is dd-mm-yyyy) df$Date <- as.Date(df$Date, format = "%d-%m-%Y") # Convert to an xts object (indexed by date) xts_eto <- xts(df$ETO, order.by = df$Date) # Now run dm2seasonal with explicit winter specification winter_results <- dm2seasonal(xts_eto, season = "winter", out.fmt = "data.frame")
2. Explicitly Define the Winter Season & Use Hydrological Year
The default "winter" setting might not align with your cross-year definition. Manually specify the months and use the type = "hydroyear" parameter to ensure the function groups December with the following January/February:
# Manually set winter months and use hydrological year grouping winter_results <- dm2seasonal( xts_eto, season = c(12, 1, 2), # Explicitly list winter months type = "hydroyear", # Treats Dec as part of the next year's winter FUN = mean, # Your aggregation function (mean, sum, etc.) na.rm = TRUE # Ignore missing values if any )
3. Check for Incomplete Winter Data
Your dataset starts in January 1951, so the first winter (1950 Dec + 1951 Jan/Feb) is missing December 1950 data. This can cause the function to return an empty dataset for that season. You can either:
- Filter out incomplete seasons after running the function, or
- Add
na.rm = TRUEto let the function skip missing values (though this will use partial data for the first winter).
Alternative: Manual Season Calculation with dplyr & lubridate
If dm2seasonal still gives you trouble, you can bypass it entirely and compute winter aggregates manually. This gives you full control over season grouping:
library(dplyr) library(lubridate) df_processed <- df %>% mutate( # Assign winter to the year of January/February (e.g., Dec 1950 belongs to Winter 1951) winter_year = ifelse(month(Date) == 12, year(Date) + 1, year(Date)), season = case_when( month(Date) %in% c(12, 1, 2) ~ "Winter", month(Date) %in% c(3, 4, 5) ~ "Spring", month(Date) %in% c(6, 7, 8) ~ "Summer", month(Date) %in% c(9, 10, 11) ~ "Autumn" ) ) %>% filter(season == "Winter") %>% group_by(winter_year) %>% summarise(avg_eto = mean(ETO, na.rm = TRUE)) # Replace with sum if needed
This approach avoids the quirks of dm2seasonal and lets you customize exactly how seasons are grouped.
内容的提问来源于stack exchange,提问作者Soumik Das

