I am trying to find the row-wise minimum from a data frame of POSIXct
. But using apply
with which.min
does not work:
df <- data.frame(date1 = as.POSIXct(c("2016-01-01", "2016-02-01")),
date2 = as.POSIXct(c("2015-10-01", "2016-06-01")))
apply(df, 1, which.min) # not OK
# integer(0)
# Warning messages:
# 1: In FUN(newX[, i], ...) : NAs introduced by coercion
# 2: In FUN(newX[, i], ...) : NAs introduced by coercion
apply(df, 1, function(x) which(x == min(x))) # OK
# [1] 2 1
sapply(1:nrow(df), function(i) which.min(df[i,])) # OK
# date2 date1
# 2 1
Can anybody explain this behaviour?