有没有办法在R中使其他变量保持不变的同时拆分时间和持续时间变量?

活泼的

我现在有一个数据集,现在想在12:00 pm(正午)分成两个数据集,即,如果变量从08:00-13:00变为08:00-12:00和12:00-13:两排为00。变量持续时间和累计和需要相应地更改,但其他变量应与原始变量相同(不变)。

这应该适用于不同的id变量。

id = unchanged from row 1, just repeated
start = changed in both rows
end = changed in both rows
day = unchanged from row 1, just repeated
duration = changed in both rows
cumulative time = changed in both row

ORIGINAL DATAFILE
#Current dataframe
id<-c("m1","m1")
x<-c("2020-01-03 10:00:00","2020-01-03 19:20:00")
start<-strptime(x,"%Y-%m-%d %H:%M:%S")
y<-c("2020-01-03 16:00:00","2020-01-03 20:50:00")
end<-strptime(y,"%Y-%m-%d %H:%M:%S")
day<-c(1,1)
mydf<-data.frame(id,start,end,day)
# calculate duration and time
mydf$duration<-as.numeric(difftime(mydf$end,mydf$start,units = "hours"))
mydf$time<-c(cumsum(mydf$duration))

REQUIRED DATAFILE
#Required dataframe
id2<-c("m1","m1","m1")
x2<-c("2020-01-03 10:00:00","2020-01-03 12:00:00","2020-01-03 19:20:00")
start2<-strptime(x2,"%Y-%m-%d %H:%M:%S")
y2<-c("2020-01-03 12:00:00","2020-01-03 16:00:00","2020-01-03 20:50:00")
end2<-strptime(y2,"%Y-%m-%d %H:%M:%S")
day2<-c(1,1,1)
mydf2<-data.frame(id2,start2,end2,day2)
# calculate duration and time
mydf2$duration<-c(2,4,1.5)
mydf2$time<-c(2,6,7.5)
DHW

好问题。因此,每行隐式包含一个或两个间隔,因此您应该能够仅在每行上定义这些间隔,然后将其旋转为long,但不能使用间隔值进行旋转(还可以吗?)。因此,这是我的方法,该方法最多为每条线计算两个班次的开始时间,然后从旋转后的下一个班次的开始推断班次的结束。内联评论。

library(lubridate, warn.conflicts = FALSE)
library(tidyverse)
library(magrittr, warn.conflicts = FALSE)
library(hablar, warn.conflicts = FALSE)

(mydf <- tibble(
  id    = "m1",
  start = as_datetime(c("2020-01-03 10:00:00", "2020-01-03 19:20:00")),
  end   = as_datetime(c("2020-01-03 16:00:00", "2020-01-03 20:50:00")),
  day   = 1
))
#> # A tibble: 2 x 4
#>   id    start               end                   day
#>   <chr> <dttm>              <dttm>              <dbl>
#> 1 m1    2020-01-03 10:00:00 2020-01-03 16:00:00     1
#> 2 m1    2020-01-03 19:20:00 2020-01-03 20:50:00     1

(mydf2 <- 
    mydf %>% 
    # Assume the relevant noontime cutoff is on the same day as the start
    mutate(midday = 
             start %>% as_date() %>% 
             add(12 %>% hours()) %>% 
             fit_to_timeline() %>% 
             # No relevant midday if the shift doesn't include noon
             na_if(not(. %within% interval(start, end)))) %>% 

    # Make an original row ID since there doesn't seem to be one, and we will need
    # to build intervals within the data stemming from each original row
    rownames_to_column("orig_shift") %>% 

    pivot_longer(cols = c(start, midday, end),
                 # The timestamps we have here will be treated as start times
                 values_to = "start",
                 # Drop rows that would exist due to irrelevant middays
                 values_drop_na = TRUE) %>% 
    select(-name) %>% 

    # Infer shift end times as the start of the next shift, within lines defined
    # by the original shifts
    group_by(orig_shift) %>% 
    arrange(start) %>% 
    mutate(end = lead(start)) %>% 
    ungroup() %>% 

    # Drop lines that represent the end of the last shift and not a full one
    drop_na() %>% 

    # Compute those durations and times (should times really be globally
    # cumulative? Also, your specified mydf2 seems to have an incorrect first time
    # value)
    mutate(duration = start %--% end %>% as.numeric("hours"),
           time = cumsum(duration)) %>% 
    select(id, start, end, day, duration, time))
#> # A tibble: 3 x 6
#>   id    start               end                   day duration  time
#>   <chr> <dttm>              <dttm>              <dbl>    <dbl> <dbl>
#> 1 m1    2020-01-03 10:00:00 2020-01-03 12:00:00     1      2     2  
#> 2 m1    2020-01-03 12:00:00 2020-01-03 16:00:00     1      4     6  
#> 3 m1    2020-01-03 19:20:00 2020-01-03 20:50:00     1      1.5   7.5

reprex软件包(v0.3.0)创建于2019-10-23

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法获取持续录音的持续时间

有没有办法从未知字节设置/清除位,同时保持所有其他位不变?

有没有办法从视频中剪切帧以减少其大小和持续时间?

有没有办法使用Python openpyxl从excel读取持续时间类型字段?

有没有办法以天为单位获得熊猫偏移别名的持续时间?

有没有办法在 Python 中执行固定持续时间的函数?

有没有办法管理在横幅上显示更长的远程通知的持续时间?

[pine-script],有没有办法“监控”“触发”警报的持续时间?

C - 有没有办法让变量在循环外保持不变?

有没有办法在 Google App Engine 中查找实例的生成时间和其他详细信息?

显示模式窗口时,有没有办法使其他窗口保持活动状态?

没有其他变量,有没有办法解决这个数学问题?

有没有办法让python交替重新分配变量,同时保持一个交替锁定?

有没有办法使CircularProgressIndicator或Future持续最短的时间(发出HTTP请求时)?

有没有办法为其他数据框中的变量组合创建带有行的数据框?

有没有办法同时拥有加密和未加密的主机变量?

有没有办法为两个时间点之间的年份创建虚拟变量?

有没有办法在 Altair 上可视化时间序列数据框,其中变量是标题?

有没有办法将变量重新分配给其他特征实现?

有没有办法将变量从Javascript中的其他模块传递给导出的生成器?

有没有办法将Range变量重新分配给其他范围?

有没有办法根据索引折叠几列,同时保留其他列?

有没有办法在扩展其他列的同时不扩展一列?

消除具有静态存储持续时间的变量

有没有办法在bash中使用变量值作为变量名?

有没有办法在变量名称中使用变量?

Spark中有没有办法保持每个阶段的运行时间?

具有自动存储持续时间的变量的地址可以在其定义中使用吗?

有没有办法在保留 JavaScript 中的变量的同时导出对象?