【问题标题】:Question Regarding creating time period/intervals in R?关于在 R 中创建时间段/间隔的问题?
【发布时间】:2020-02-09 11:58:08
【问题描述】:

我在 R 中创建时间段时遇到问题。

我手头的数据是enter image description here

现在我想做以下事情。

确定开始时间和结束时间之间的小时间隔并创建一个列表

确定开始的小时间隔和休息时间

最后,去掉休息区间,求总时间

然后创建一个输出。

你能帮帮我吗?

【问题讨论】:

  • 发帖时请按照r标签顶部的说明进行操作。

标签: r time intervals lubridate duration


【解决方案1】:

我不完全确定您到底需要什么,但据我了解,这里有一些东西可以帮助您:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date


t <- tibble(
  place = "xyz", 
  st = "9:00", 
  et = "13:00"
)


t  <- t %>% 
  mutate(stdur = period_to_seconds(hm(st))) %>% 
  mutate(etdur = period_to_seconds(hm(et))) %>% 
  mutate(interval = dseconds(etdur - stdur)) %>% 
  mutate(interval_hours = seconds_to_period(interval))


glimpse(t)
#> Observations: 1
#> Variables: 7
#> $ place          <chr> "xyz"
#> $ st             <chr> "9:00"
#> $ et             <chr> "13:00"
#> $ stdur          <dbl> 32400
#> $ etdur          <dbl> 46800
#> $ interval       <Duration> 14400s (~4 hours)
#> $ interval_hours <Period> 4H 0M 0S
Created on 2020-02-10 by the reprex package (v0.3.0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多