微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用带有 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 举报,一经查实,本站将立刻删除。