R:为一天中的时间数据分配正确的日期

标签 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: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
}

这给出了所需的结果,然后我可以使用它来创建正确的 POSIXlt 日期列(包括日期和时间)。

    V1        V2        day
1   11:40:34    1   2010-10-14
2   11:52:28    2   2010-10-14
3   23:37:19    37  2010-10-14
4   23:57:27    38  2010-10-14
5   00:17:35    39  2010-10-15
6   00:37:43    40  2010-10-15
7   00:57:52    41  2010-10-15
8   01:18:00    42  2010-10-15
9   04:17:03    52  2010-10-15
10  17:47:29    18  2010-10-15
11  21:29:00    29  2010-10-15
12  23:49:58    36  2010-10-15
13  00:10:06    37  2010-10-16
14  00:30:14    38  2010-10-16
15  11:14:39    70  2010-10-16
16  18:20:09    1   2010-10-16
17  23:28:15    17  2010-10-16
18  23:48:24    18  2010-10-16
19  00:08:32    19  2010-10-17
20  00:28:40    20  2010-10-17
21  02:49:38    27  2010-10-17
22  08:11:51    43  2010-10-17
23  15:14:45    64  2010-10-17
24  17:15:34    70  2010-10-17

不过,我想摆脱循环,并且希望了解有关如何更加“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))

关于R:为一天中的时间数据分配正确的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11959763/

相关文章:

r - 使用最小/最大条件公式并在数据框中创建新列

R 预订 : Tabbed headings

date - 如何在MVEL中为ElasticSearch将毫秒转换为日期

bash - 使用时间命令进行基准测试

c - 如何查看linux内核是否会在月底插入闰秒

php - 如何转换Y-m-d H :i:s to Y-m-d in PHP?

r - 显示跨一条显示行的循环计数器

R 传单提供商平铺 basemap - 传递 HERE 的 API 代码 token

date - CMake转换日期格式

php - 开始日期和开始时间小于结束日期和结束时间验证