Kyle Dixon
Kyle Dixon

Reputation: 305

min() function not working on date objects in R

I have a working solution to this but I'd really like to understand what is causing this issue in the first place. I have a data frame named 'ngx' with a date column named 'DeliveryDate'.

enter image description here

When I try to find the minimum date of a subset of this data frame:

min(ngx[which(ngx$TradedIndexID == 4810), 2])

I run into this error:

Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables

But when I tweak the selection of the 'DeliveryDate' column to:

min(ngx[which(ngx$TradedIndexID == 4810), ]$DeliveryDate)

It works just fine:

[1] "2019-08-01"

Does anyone know the underlying issue causing this?

Upvotes: 1

Views: 883

Answers (1)

Thomas
Thomas

Reputation: 1302

As you mentioned in the comment, your data is of type tibble. That means you could solve the problem by

  1. transforming the data to data.frame prior to your calculations: as.data.frame(ngx)
  2. doing the same in the tidyverse way:
    ngx %>% filter(TradedIndexID == 4810) %>% summarise(Min = min(DeliveryDate))

Upvotes: 2

Related Questions