转换时间时有关 NA 值的警告

Warning regarding NA values while converting time

我有代码并且可以正常工作:

library(tidyverse)
library(lubridate)

df<-data.frame("id"=c(1,2,3,4,5), "Time"=c("1999-12-31 10:10:10","1999-12-31 09:05:13","1999-12-31 00:05:25","1999-12-31 00:07:04","1999-12-31 03:05:07"))
tib<-as_tibble(df)

time_converted_data_1<-tib
time_converted_data_2<-tib

time_converted_data_1$Time<-unlist(lapply(tib$Time, function(x) period_to_seconds(hms(paste(hour(x), minute(x), second(x), sep = ":")))))

time_converted_data_2$Time<-period_to_seconds(hms(paste(hour(tib$Time), minute(tib$Time), second(tib$Time), sep = ":")))

但是如果我改变:

df<-data.frame("id"=c(1,2,3,4,5), "Time"=c("1999-12-31 10:10:10","1999-12-31 09:05:13","1999-12-31 00:05:25","1999-12-31 00:07:04","1999-12-31 03:05:07"))

到(将最后一个值更改为 NA):

df<-data.frame("id"=c(1,2,3,4,5), "Time"=c("1999-12-31 10:10:10","1999-12-31 09:05:13","1999-12-31 00:05:25","1999-12-31 00:07:04", NA))

比我收到警告:

Warning messages:
1: In .parse_hms(..., order = "HMS", quiet = quiet) :
  Some strings failed to parse, or all strings are NAs
2: In .parse_hms(..., order = "HMS", quiet = quiet) :
  Some strings failed to parse, or all strings are NAs

我尝试以不同的方式使用 is.na(),但没有成功。

问题:这个警告会影响计算吗?现在使用安全吗?

只有在您认为所有值都是合法的情况下,该警告才会令人担忧:根据该假设,该警告可以识别出数据中间某处 已损坏 .对于更大的数据集(可能是实时的或未整理的),这可能是数据中其他内容的指标。

总的来说,警告是有的,因为作者认为有一种情况你可能没有意识到,可能会影响整体processing/analysis的correctness/completeness。

但是,在这种情况下,我怀疑 您并不感到惊讶,在这种情况下,您可以使用一种技术来避免该特定警告。存在 抑制 所有警告 的其他技术,但这有点核,会掩盖您尚未发现的其他问题。总的来说,我喜欢避免已知问题,这样您就不会 "numb" 收到警告并开始忽略它们。

### "normal" way
tib$Time1 <- period_to_seconds(hms(paste(hour(tib$Time), minute(tib$Time), second(tib$Time), sep = ":")))
# Warning in .parse_hms(..., order = "HMS", quiet = quiet) :
#   Some strings failed to parse, or all strings are NAs
tib$Time1
# [1] 36610 32713   325   424    NA

### *avoid* the warning, no suppression, just not trying to parse "known-NA"
tib$Time2 <- NA
notna <- !is.na(tib$Time)
tib$Time2[notna] <- period_to_seconds(hms(paste(hour(tib$Time[notna]),
  minute(tib$Time[notna]), second(tib$Time[notna]), sep = ":")))
tib$Time2
# [1] 36610 32713   325   424    NA