Skip to content

cheetahR fails to handle POSIXct (datetime) columns #48

@akins11

Description

@akins11

First, I want to say a thank you for developing and maintaining this package, it’s been incredibly helpful.

Description

The cheetahR::cheetah() function fails when processing data frames containing POSIXct (e.i datetime) columns, while it works correctly with Date columns. I believe the error occurs in the column class checking logic.

Error Message

Error in if (col_classes[[col_name]] %in% c("numeric", "integer")) { : the condition has length > 1

The Likely Cause

The issue originate from how R handles class attributes for different date/time types:

  • Date objects have a single class: class(as.Date("2025-01-01")) returns "Date"
  • POSIXct objects have multiple classes: class(as.POSIXct("2025-01-01")) returns c("POSIXct", "POSIXt")

The current code uses:

if (col_classes[[col_name]] %in% c("numeric", "integer")) {...}
When col_classes[[col_name]] has length > 1 (as with POSIXct), the %in% operator returns a logical vector of the same length, causing the if condition to fail with "condition has length > 1".

Reproducible Example

library(tibble)
library(dplyr)
library(cheetahR)

# This code works:
create_date_ranges <- function(start_date = Sys.Date()) {
  
  # Daily dates (100 consecutive days)
  daily_dates <- seq.Date(
    from = start_date,
    length.out = 100,
    by = "day"
  )
  
  # Weekly dates (100 weeks)
  weekly_dates <- seq.Date(
    from = start_date,
    length.out = 100,
    by = "week"
  )
  
  # Monthly dates (100 months)
  monthly_dates <- seq.Date(
    from = start_date,
    length.out = 100,
    by = "month"
  )
  
  # Create tibble
  date_tibble <- tibble(
    id = 1:100,
    daily = daily_dates,
    weekly = weekly_dates,
    monthly = monthly_dates
  )
  
  return(date_tibble)
}

date_df <- create_date_ranges()

# These work fine
cheetahR::cheetah(data = date_df)
cheetahR::cheetah(data = select(date_df, daily))
cheetahR::cheetah(data = select(date_df, weekly))
cheetahR::cheetah(data = select(date_df, monthly))

# This does not work
create_datetime_ranges <- function(start_date = Sys.Date()) {
  
  # Daily dates (100 consecutive days) - convert to datetime
  daily_dates <- seq.Date(
    from = start_date,
    length.out = 100,
    by = "day"
  )
  daily_datetime <- as.POSIXct(daily_dates, tz = "UTC")
  
  # Weekly dates (100 weeks) - convert to datetime
  weekly_dates <- seq.Date(
    from = start_date,
    length.out = 100,
    by = "week"
  )
  weekly_datetime <- as.POSIXct(weekly_dates, tz = "UTC")
  
  # Monthly dates (100 months) - convert to datetime
  monthly_dates <- seq.Date(
    from = start_date,
    length.out = 100,
    by = "month"
  )
  monthly_datetime <- as.POSIXct(monthly_dates, tz = "UTC")
  
  # Create tibble
  date_tibble <- tibble(
    id = 1:100,
    daily = daily_datetime,
    weekly = weekly_datetime,
    monthly = monthly_datetime
  )
  
  return(date_tibble)
}

datetime_df <- create_datetime_ranges()

# These all fail with the error above
cheetahR::cheetah(data = select(datetime_df, daily))
cheetahR::cheetah(data = select(datetime_df, weekly))
cheetahR::cheetah(data = select(datetime_df, monthly))

Environment

  • R version: R version 4.5.1 (2025-06-13 ucrt)
  • cheetahR version: 0.2.0
  • Platform:
platform       x86_64-w64-mingw32               
arch           x86_64                           
os             mingw32                          
crt            ucrt                             
system         x86_64, mingw32

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions