R语言嵌套ifelse处理字符、时间日期变量时出错求助
Let's break down what's going wrong with your code and fix it step by step, plus I'll show a cleaner alternative that avoids messy nested ifelse calls.
First, the root causes of your error
- Incorrect
ifelsesyntax:ifelse()only takes 3 arguments:condition,yes,no. You tried passing a 4th argument to the firstifelse, which is why you got the "unused argument" error. Your nested structure was misaligned. - Incomplete
difftimecalls: For Frankfurt and Hamburg, you forgot to compare the closing time to theHMSvalue—you only passed one time todifftime, which is invalid. - Variable name mismatch: Your data frame has
StockExchange(singular), but your code usesStockExchanges(plural) — that would cause unexpectedFALSEvalues. - Mixing
dfanddf1: You start modifyingdf$TimeStampNewbut then referencedf1later; keep your data frame names consistent. - NA handling can be integrated: No need for a separate line to handle NA
HMSvalues—we can include that in our logic.
Fixed nested ifelse code
First, let's correct the syntax and fix all the issues:
# Ensure variable names match your data frame (StockExchange, not StockExchanges) df$TimeStampNew <- with(df, ifelse(is.na(HMS), # Handle NA HMS values first as.character(TimeStamp), ifelse(StockExchange == "Vienna" & difftime(as.POSIXct(HMS, format = "%H:%M:%S"), as.POSIXct("17:35:00", format = "%H:%M:%S")) > 0, as.character(TimeStamp + 1), ifelse(StockExchange == "Frankfurt" & difftime(as.POSIXct(HMS, format = "%H:%M:%S"), as.POSIXct("22:00:00", format = "%H:%M:%S")) > 0, as.character(TimeStamp + 1), ifelse(StockExchange == "Hamburg" & difftime(as.POSIXct(HMS, format = "%H:%M:%S"), as.POSIXct("20:00:00", format = "%H:%M:%S")) > 0, as.character(TimeStamp + 1), as.character(TimeStamp)) ) ) ) )
A cleaner alternative using dplyr::case_when
Nested ifelse gets hard to read quickly. Using case_when from the dplyr package makes this logic much more explicit and maintainable:
library(dplyr) df <- df %>% mutate( TimeStampNew = case_when( # Keep original timestamp if HMS is NA is.na(HMS) ~ as.character(TimeStamp), # Vienna: add 1 day if HMS is later than 17:35 StockExchange == "Vienna" & difftime(as.POSIXct(HMS, format = "%H:%M:%S"), as.POSIXct("17:35:00", format = "%H:%M:%S")) > 0 ~ as.character(TimeStamp + 1), # Frankfurt: add 1 day if HMS is later than 22:00 StockExchange == "Frankfurt" & difftime(as.POSIXct(HMS, format = "%H:%M:%S"), as.POSIXct("22:00:00", format = "%H:%M:%S")) > 0 ~ as.character(TimeStamp + 1), # Hamburg: add 1 day if HMS is later than 20:00 StockExchange == "Hamburg" & difftime(as.POSIXct(HMS, format = "%H:%M:%S"), as.POSIXct("20:00:00", format = "%H:%M:%S")) > 0 ~ as.character(TimeStamp + 1), # Default: keep original timestamp for all other cases TRUE ~ as.character(TimeStamp) ) )
Both solutions handle all your requirements: NA values, exchange-specific closing time checks, and date incrementing when needed. The case_when version is far easier to edit if you add more exchanges or adjust closing times later.
内容的提问来源于stack exchange,提问作者Chellarioo
相关产品推荐
相关产品推荐

