【发布时间】:2016-08-04 08:39:59
【问题描述】:
我试图从POSIXct 的数据框中找到逐行最小值。但是使用apply 和which.min 不起作用:
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
谁能解释这种行为?
【问题讨论】:
-
我认为
apply将df[1,]转换为as.matrix,这将导致值是which.min无法解释的字符类型。