Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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 - Fatal编程技术网

R 将日期(时间)变量重新编码为新的离散变量

R 将日期(时间)变量重新编码为新的离散变量,r,R,我有时间变量:“00:00:29”、“00:06:39”、“20:43:15”… 我想重新编码到新的基于向量时间的工作班次: 07:00:00 - 13:00:00 - 1 13:00:00 - 20:00:00 - 2 23:00:00 - 7:00:00 - 3 谢谢你的建议:)假设时间变量是如图所示的字符串,这似乎是可行的: secNr <- function(x){ sum(as.numeric(unlist(strsplit(x,":",fixed=TRU

我有时间变量:
“00:00:29”、“00:06:39”、“20:43:15”…
我想重新编码到新的基于向量时间的工作班次:

07:00:00 - 13:00:00   -  1 
13:00:00 - 20:00:00   -  2
23:00:00 - 7:00:00    -  3

谢谢你的建议:)

假设时间变量是如图所示的字符串,这似乎是可行的:

secNr <- function(x){ sum(as.numeric(unlist(strsplit(x,":",fixed=TRUE))) * c(3600,60,1)) }

workShift <- function(x)
{
  n <- which.max(secNr(x) >= c(secNr("23:00:00"),secNr("20:00:00"),secNr("13:00:00"),secNr("07:00:00"),secNr("00:00:00")))
  c(3,NA,2,1,3)[n]
}

secNr假设时间变量是如图所示的字符串,这似乎有效:

secNr <- function(x){ sum(as.numeric(unlist(strsplit(x,":",fixed=TRUE))) * c(3600,60,1)) }

workShift <- function(x)
{
  n <- which.max(secNr(x) >= c(secNr("23:00:00"),secNr("20:00:00"),secNr("13:00:00"),secNr("07:00:00"),secNr("00:00:00")))
  c(3,NA,2,1,3)[n]
}

secNr您已经尝试了什么?为什么不起作用?我不知道怎么做,我是r的新手。你已经试过什么了?为什么不起作用呢?我不知道怎么做,我是r的新手。