Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何基于多个条件延迟日期,并在延迟发生后重置_R_Loops_If Statement_Lag - Fatal编程技术网

R 如何基于多个条件延迟日期,并在延迟发生后重置

R 如何基于多个条件延迟日期,并在延迟发生后重置,r,loops,if-statement,lag,R,Loops,If Statement,Lag,我有一个车站维修的数据框 工作流程是这样的:机械师去一个站点,按下一个按钮,记录一个名为release的动作。在他们修复工作站后,再次按下按钮,操作现在是return 您可以在下面看到第1行和第2行是一项已完成的任务,需要Jane Jetson10秒才能完成 dt name foo_id foo_role bikeId station_name station_id action 1 2019-12-12 13:05:47 Jane Jets

我有一个车站维修的数据框

工作流程是这样的:机械师去一个站点,按下一个按钮,记录一个名为
release
动作
。在他们修复工作站后,再次按下按钮,操作现在是
return

您可以在下面看到
第1行
第2行
是一项已完成的任务,需要
Jane Jetson
10秒才能完成

                   dt        name foo_id foo_role bikeId station_name station_id  action
1 2019-12-12 13:05:47 Jane Jetson 106337 Mechanic  12345   FooStation    1234.89 Release
2 2019-12-12 13:05:57 Jane Jetson 106337 Mechanic  12345   FooStation    1234.89  Return
3 2019-12-12 13:06:16    John Doe 106338 Mechanic  12345   FooStation    1234.89 Release
4 2019-12-12 13:06:19    John Doe 106338 Mechanic  12345   FooStation    1234.89  Return
5 2019-12-12 13:07:16    John Doe 106338 Mechanic  12345   FooStation    1234.89 Release
6 2019-12-12 14:07:16    John Doe 106338 Mechanic  56789 Some Station    4567.12 Release
我希望发生什么:

structure(list(dt = structure(c(1576173947, 1576173957, 1576173976, 
1576173979, 1576174036, 1576177636), class = c("POSIXct", "POSIXt"
), tzone = ""), name = structure(c(1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Jane Jetson", 
"John Doe"), class = "factor"), foo_id = c(106337L, 106337L, 
106338L, 106338L, 106338L, 106338L), foo_role = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "Mechanic", class = "factor"), 
    bikeId = c(12345L, 12345L, 12345L, 12345L, 12345L, 56789L
    ), station_name = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("FooStation", 
    "Some Station"), class = "factor"), station_id = c(1234.89, 
    1234.89, 1234.89, 1234.89, 1234.89, 4567.12), action = c("Release", 
    "Return", "Release", "Return", "Release", "Release")), row.names = c(NA, 
-6L), class = "data.frame")
  • 我想知道每个
    技工使用
    操作
    释放
    然后执行
    返回
    来修理工作站需要多长时间
  • 如果
    版本
    没有
    返回
    ,我想取
    Sys.time()
    并从
    dt
    中减去它。您将看到
    第5行
    第6行
我这样做了:(我不是100%确定我需要上一个动作,但我把它包括在内以防需要。)


使用
dplyr
的一种方法是为每个
name
(或
foo\u id
)和每次发生
“Release”
时创建组。在该组中,如果存在
'Return'
,我们计算
'Return'
'Release'
之间的时间差,或者计算
'Release'
与当前时间之间的差

library(dplyr)

df %>%
  group_by(name, group = cumsum(action == "Release")) %>%
  mutate(timediffsecs = if (any(action == 'Return')) 
            dt[action == 'Return'] - dt[action == 'Release'] else Sys.time() - dt, 
         #If we want to replace Release values with NA
         timediffsecs  = replace(timediffsecs, n() > 1 & action == 'Release', NA))


#   dt                  name        foo_id foo_role bikeId station_name station_id action  group timediffsecs
#  <dttm>              <fct>        <int> <fct>     <int> <fct>             <dbl> <chr>   <int> <drtn>      
#1 2019-12-13 02:05:47 Jane Jetson 106337 Mechanic  12345 FooStation        1235. Release     1     NA secs 
#2 2019-12-13 02:05:57 Jane Jetson 106337 Mechanic  12345 FooStation        1235. Return      1     10 secs 
#3 2019-12-13 02:06:16 John Doe    106338 Mechanic  12345 FooStation        1235. Release     2     NA secs 
#4 2019-12-13 02:06:19 John Doe    106338 Mechanic  12345 FooStation        1235. Return      2      3 secs 
#5 2019-12-13 02:07:16 John Doe    106338 Mechanic  12345 FooStation        1235. Release     3 472603 secs 
#6 2019-12-13 03:07:16 John Doe    106338 Mechanic  56789 Some Station      4567. Release     4 469003 secs 
库(dplyr)
df%>%
分组人(名称,组=cumsum(操作==“发布”))%>%
mutate(timediffsecs=if(any(action=='Return'))
dt[action=='Return']-dt[action=='Release']else Sys.time()-dt,
#如果我们想用NA替换发布值
timediffsecs=replace(timediffsecs,n()>1&action='Release',NA))
#dt name foo_id foo_角色bikeId station name station\u id动作组timediffsecs
#                                                       
#2019年12月13日02:05:47简·杰森106337机械师12345食品站1235。释放1 NA秒
#2019-12-13 02:05:57简·杰森106337机修工12345食品站1235。返回10秒
#3 2019-12-13 02:06:16约翰·多伊106338技工12345,1235车站。发布2秒
#4 2019-12-13 02:06:19无名氏106338技工12345,1235车站。返回2 3秒
#5 2019-12-13 02:07:16无名氏106338技工12345,1235车站。释放3 472603秒
#6 2019-12-13 03:07:16约翰·多伊106338机修工56789某车站4567。第4版469003秒

虽然这对给定的示例有效,但可能需要根据数据进行一些调整

您如何将
发布
返回
匹配?它们是否总是在连续的行中互相跟随?现在我正在分组,不包括操作。它并不总是连续的,但是在一个站点上的发布在返回之前不会再次发生。
structure(list(dt = structure(c(1576173947, 1576173957, 1576173976, 
1576173979, 1576174036, 1576177636), class = c("POSIXct", "POSIXt"
), tzone = ""), name = structure(c(1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Jane Jetson", 
"John Doe"), class = "factor"), foo_id = c(106337L, 106337L, 
106338L, 106338L, 106338L, 106338L), foo_role = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "Mechanic", class = "factor"), 
    bikeId = c(12345L, 12345L, 12345L, 12345L, 12345L, 56789L
    ), station_name = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("FooStation", 
    "Some Station"), class = "factor"), station_id = c(1234.89, 
    1234.89, 1234.89, 1234.89, 1234.89, 4567.12), action = c("Release", 
    "Return", "Release", "Return", "Release", "Release")), row.names = c(NA, 
-6L), class = "data.frame")
library(dplyr)

df %>%
  group_by(name, group = cumsum(action == "Release")) %>%
  mutate(timediffsecs = if (any(action == 'Return')) 
            dt[action == 'Return'] - dt[action == 'Release'] else Sys.time() - dt, 
         #If we want to replace Release values with NA
         timediffsecs  = replace(timediffsecs, n() > 1 & action == 'Release', NA))


#   dt                  name        foo_id foo_role bikeId station_name station_id action  group timediffsecs
#  <dttm>              <fct>        <int> <fct>     <int> <fct>             <dbl> <chr>   <int> <drtn>      
#1 2019-12-13 02:05:47 Jane Jetson 106337 Mechanic  12345 FooStation        1235. Release     1     NA secs 
#2 2019-12-13 02:05:57 Jane Jetson 106337 Mechanic  12345 FooStation        1235. Return      1     10 secs 
#3 2019-12-13 02:06:16 John Doe    106338 Mechanic  12345 FooStation        1235. Release     2     NA secs 
#4 2019-12-13 02:06:19 John Doe    106338 Mechanic  12345 FooStation        1235. Return      2      3 secs 
#5 2019-12-13 02:07:16 John Doe    106338 Mechanic  12345 FooStation        1235. Release     3 472603 secs 
#6 2019-12-13 03:07:16 John Doe    106338 Mechanic  56789 Some Station      4567. Release     4 469003 secs