Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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_Date_Time - Fatal编程技术网

R:为时间数据指定正确的日期

R:为时间数据指定正确的日期,r,date,time,R,Date,Time,我有日志数据文件,只存储一天中的某个时间以及连续几天的一些数据 V1 V2 1 11:40:34 1 2 11:52:28 2 3 23:37:19 37 4 23:57:27 38 5 00:17:35 39 6 00:37:43 40 7 00:57:52 41 8 01:18:00 42 9 04:17:03 52 10 17:47:29 18 11 21:29:0

我有日志数据文件,只存储一天中的某个时间以及连续几天的一些数据

       V1       V2
1   11:40:34    1
2   11:52:28    2
3   23:37:19    37
4   23:57:27    38
5   00:17:35    39
6   00:37:43    40
7   00:57:52    41
8   01:18:00    42
9   04:17:03    52
10  17:47:29    18
11  21:29:00    29
12  23:49:58    36
13  00:10:06    37
14  00:30:14    38
15  11:14:39    70
16  18:20:09    1
17  23:28:15    17
18  23:48:24    18
19  00:08:32    19
20  00:28:40    20
21  02:49:38    27
22  08:11:51    43
23  15:14:45    64
24  17:15:34    70
我知道文件创建时的“开始日期”,需要为每一行指定正确的日期。我想出了一个使用循环的方法:

data <- structure(list(V1 = structure(
          c(13L, 14L, 21L, 24L, 3L, 6L, 7L, 
           8L, 10L, 17L, 19L, 23L, 2L, 5L, 
           12L, 18L, 20L, 22L, 1L, 4L, 9L, 
           11L, 15L, 16L), .Label = 
           c("00:08:32", "00:10:06", "00:17:35", 
           "00:28:40", "00:30:14", "00:37:43", "00:57:52", "01:18:00", "02:49:38", 
           "04:17:03", "08:11:51", "11:14:39", "11:40:34", "11:52:28", "15:14:45", 
           "17:15:34", "17:47:29", "18:20:09", "21:29:00", "23:28:15", "23:37:19", 
           "23:48:24", "23:49:58", "23:57:27"), class = "factor"), 
            V2 = c(1L, 2L, 37L, 38L, 39L, 40L, 41L, 42L, 52L, 18L, 29L, 36L, 37L, 38L, 
           70L, 1L, 17L, 18L, 19L, 20L, 27L, 43L, 64L, 70L)), .Names = 
           c("V1", "V2"), class = "data.frame", row.names = c(NA, -24L))

startdate <- as.Date("2010-10-14")
data$day <- startdate
k <- 0

for (i in 1:length(data$V1)-1){
  if (as.POSIXlt(paste(startdate,data$V1[i],sep=" ")) > 
      as.POSIXlt(paste(startdate,data$V1[i+1],sep=" "))) 
      k <- k+1
  data$day[i+1] <- startdate + k
}

不过,我想摆脱这个循环,并希望有任何关于如何更“R风格”地完成此任务的想法。

有点草率,但我认为这将完成工作

startdate <- as.Date("2010-10-14")
f=as.POSIXlt(data$V1,format='%H:%M:%S')
x=diff(c(0,which(c(f[-length(f)] > f[-1],T))))
y=rep(seq(x),x)-1
as.POSIXlt(paste(startdate+y,data$V1))
startdate f[-1],T)))
y=代表(顺序(x),x)-1
as.POSIXlt(粘贴(startdate+y,数据$V1))
startdate <- as.Date("2010-10-14")
f=as.POSIXlt(data$V1,format='%H:%M:%S')
x=diff(c(0,which(c(f[-length(f)] > f[-1],T))))
y=rep(seq(x),x)-1
as.POSIXlt(paste(startdate+y,data$V1))