【问题标题】:Difftime returns 0 even there is a clear time difference即使有明显的时差,Difftime 也会返回 0
【发布时间】:2021-07-29 07:59:19
【问题描述】:

Difftime 总是返回 0 值,即使有时间上的差异。

Combined_data$ride_length <- difftime(Combined_data$ended_at, Combined_data$started_at)

/////////////////////////////////////// /////////////////////////////////////////////p>

str(Combined_data)
'data.frame':   4073561 obs. of  19 variables:
 $ ride_id           : chr  "8CD5DE2C2B6C4CFC" "9A191EB2C751D85D" "F37D14B0B5659BCF" "C41237B506E85FA1" ...
 $ rideable_type     : chr  "docked_bike" "docked_bike" "docked_bike" "docked_bike" ...
 $ started_at        : chr  "2020-06-13 23:24:48" "2020-06-26 07:26:10" "2020-06-23 17:12:41" "2020-06-20 01:09:35" ...
 $ ended_at          : chr  "2020-06-13 23:36:55" "2020-06-26 07:31:58" "2020-06-23 17:21:14" "2020-06-20 01:28:24" ...
 $ start_station_name: chr  "Wilton Ave & Belmont Ave" "Federal St & Polk St" "Daley Center Plaza" "Broadway & Cornelia Ave" ...
 $ start_station_id  : chr  "117" "41" "81" "303" ...
 $ end_station_name  : chr  "Damen Ave & Clybourn Ave" "Daley Center Plaza" "State St & Harrison St" "Broadway & Berwyn Ave" ...
 $ end_station_id    : chr  "163" "81" "5" "294" ...
 $ start_lat         : num  41.9 41.9 41.9 41.9 41.9 ...
 $ start_lng         : num  -87.7 -87.6 -87.6 -87.6 -87.7 ...
 $ end_lat           : num  41.9 41.9 41.9 42 41.9 ...
 $ end_lng           : num  -87.7 -87.6 -87.6 -87.7 -87.7 ...
 $ member_casual     : chr  "casual" "member" "member" "casual" ...
 $ date              : Date, format: "2020-06-13" "2020-06-26" "2020-06-23" "2020-06-20" ...
 $ month             : chr  "Jun" "Jun" "Jun" "Jun" ...
 $ year              : chr  "2020" "2020" "2020" "2020" ...
 $ day               : chr  "13" "26" "23" "20" ...
 $ day_of_week       : chr  "Saturday" "Friday" "Tuesday" "Saturday" ...
 $ ride_length       : 'difftime' num  0 0 0 0 ...

【问题讨论】:

  • 我认为问题在于您的列是“chr”类型而不是日期类型,因此您应该转换它然后它应该可以工作
  • 您的日期变量存储为字符列,您需要先转换为日期时间对象。
  • 将 end_at 和 started_at 列从 "chr" 更改为 datetime ?

标签: r difftime


【解决方案1】:

您可以使用as.POSIXct将字符转换为POSIXct类型,然后使用difftime

Combined_data$ended_at <- as.POSIXct(Combined_data$ended_at, tz = 'UTC')
Combined_data$started_at <- as.POSIXct(Combined_data$started_at, tz = 'UTC')
Combined_data$ride_length <- difftime(Combined_data$ended_at, Combined_data$started_at)

如果您需要 tidyverse 替代方案 -

library(dplyr)
library(lubridate)

Combined_data <- Combined_data %>%
  mutate(across(c(started_at, ended_at), ymd_hms), 
         ride_length = difftime(ended_at,started_at)) 

【讨论】:

  • 是的,它有帮助。将 char 更改为 POSIXct。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2012-12-08
相关资源
最近更新 更多