You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 ifelse syntax: ifelse() only takes 3 arguments: condition, yes, no. You tried passing a 4th argument to the first ifelse, which is why you got the "unused argument" error. Your nested structure was misaligned.
  • Incomplete difftime calls: For Frankfurt and Hamburg, you forgot to compare the closing time to the HMS value—you only passed one time to difftime, which is invalid.
  • Variable name mismatch: Your data frame has StockExchange (singular), but your code uses StockExchanges (plural) — that would cause unexpected FALSE values.
  • Mixing df and df1: You start modifying df$TimeStampNew but then reference df1 later; keep your data frame names consistent.
  • NA handling can be integrated: No need for a separate line to handle NA HMS values—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:05:55