简体   繁体   English

为什么 diff.time 返回 NA 值,控制台显示没有错误。?

[英]Why does diff.time Return NA values, console shows no error.?

I am here trying to calculate the time difference between two date formats, first, I have been faced with various errors such as我在这里尝试计算两种日期格式之间的时间差,首先,我遇到了各种错误,例如

  • as.POSIXlt.character : character string is not in a standard unambiguous format as.POSIXlt.character :字符串不是标准的明确格式
  • Values unable to parse无法解析的值
  • NA values added by coercion通过强制添加的NA

however, with trials, I have reached this next line of code that runs with no errors or warnings but the return values are all NA但是,通过试验,我已经达到了下一行代码,它运行时没有错误或警告,但返回值都是NA

Rides_cleaned <-
  Rides %>% 
  drop_na() %>% 
  distinct() %>% 
  mutate(rideduration=difftime(as.numeric(as.POSIXct(ended_at,format = "%m/%d/%Y %H:%M:%S %p")),
                               as.numeric(as.POSIXct(started_at,format = "%m/%d/%Y %H:%M:%S %p")), units = "mins"))

在此处输入图像描述

Make sure to use the correct date format, as.numeric isn't needed either:确保使用正确的日期格式,也不需要as.numeric

Rides <- data.frame(started_at="6/4/21 7:45",ended_at="6/4/21 7:46")

Rides %>% 
  drop_na() %>% 
  distinct() %>% 
  mutate(rideduration=difftime(as.POSIXct(ended_at,format = "%m/%d/%y %H:%M"), 
                               as.POSIXct(started_at,format = "%m/%d/%y %H:%M"), 
                               units = "mins"))

#   started_at    ended_at rideduration
#1 6/4/21 7:45 6/4/21 7:46       1 mins

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM