使用带有 R 的 lubridate 将时间段转换为时间

问题描述

我编辑了我的帖子以包含一些数据并将内容分解。

我正在尝试在时间序列中填充一些缺失的时间值。为此,我想获取缺失值前后两条记录之间的时间差,并使用它来创建新时间。当我尝试以下操作(在循环中)时,出现无法将句点转换为时间的错误。 (在此代码下方,我添加了一些数据和一个玩具示例)

bottom$GPS_UTC_time_hms[ii] =   
  as.period(
    bottom$GPS_UTC_time_hms[ii-1] + 
    as.duration(
      (interval(
         bottom$GPS_UTC_time_hms[ii-1],bottom$GPS_UTC_time_hms[ii+1]
      ))/2
    )
   )

Error: Can't convert <Period> to <time>.


## When building this example,there is an additional error that appears to
## have to do with supplying an origin. It seems to be supplied with
## lubridate ("1970-01-01 UTC"),however the error asks for me to supply
## the origin and I can't figure out how to do that

library(lubridate)

# First I read data with times in a string format and a missing value
a = c("11:18:09",NA,"11:18:21" )

# Next I used lubridate to convert them to hour-minute-second format
a1 = hms(a)
# Warning message:
# In .parse_hms(...,order = "HMS",quiet = quiet) :
#   Some strings Failed to parse,or all strings are NAs
a1
# [1] "11H 18M 9S"  NA            "11H 18M 21S"


# What I would like to do is find the midpoint between the times 1 and 3 so  # that I can assign that midpoint to the missing value for time 2 (i.e.,"11H # 18M 15S")

# So,following an example I found online,I attempted to add the half         # duration of the interval between the two dates to the period from the first # value. 
# (So,"11H 18M 15S")
ap = as.period(a1[1]) 
ap
#[1] "11H 18M 9S"

# Below I get an error about needing the origin to be applied
ab_int = interval(a1[1],b1[3],tzone = "UTC")
# Error in as.POSIXct.numeric(x,tz = tz) : 'origin' must be supplied

# When I do things broken out like this,I don't get to these last two steps
# because of the error above
ab_dur = as.duration(ab_int)/2

a1[2] = ap + ab_dur

# this last step has an additional error when not done in this simplified    # manner. In the full code - which I originally posted above - I am trying to # put this value back into the GPS_UTC_time_hms variable,and I think that is # where the original mismatch error came into play: 

# Error: Can't convert <Period> to <time>.
# In addition: Warning message:
# tz(): Don't kNow how to compute timezone for object of class hms/difftime; returning "UTC". This warning will become an error in the next major version of lubridate.

# In this toy version,I never get to this error step because of the origin error.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)