使用R

时间:2018-11-01 07:46:24

标签: r lubridate

我正在尝试分析一系列大型csv文件,这些文件每隔3秒左右用R采样一次数据。其中一列是实验记录的时间戳,文件名包含执行特定实验的日期。

我正在尝试在时间戳上附加日期信息。自然地,这将涉及将日期信息和时间信息进行组合,然后将其转换为R中lubridate库中的ymd_hms对象。

这里的挑战:有时,实验是在午夜之后进行的,并且数据文件没有被它分开。这就是我的意思:

>practice[50:55, ]
   time.sub         hms hours
50 23:59:53 23H 59M 53S    23
51 23:59:55 23H 59M 55S    23
52 23:59:57 23H 59M 57S    23
53 23:59:59 23H 59M 59S    23
54    0:0:1          1S     0
55    0:0:3          3S     0

practice$hmshms(practice$time.sub)的结果,而practice$hourshours(practice$hms)的结果。

假设此数据是从181010获得的。我希望能够为181011之后的时间戳自动分配23:59:59

我想要的输出如下:

>after_some_smart_thing()
   time.sub         hms hours   date
50 23:59:53 23H 59M 53S    23 181010
51 23:59:55 23H 59M 55S    23 181010
52 23:59:57 23H 59M 57S    23 181010
53 23:59:59 23H 59M 59S    23 181010
54    0:0:1          1S     0 181011
55    0:0:3          3S     0 181011

目前我能想到的最好的主意是运行一个for循环,将hours的每个元素与其上方的元素进行比较,如果小时数减少,则在日期上加1。

其中的伪代码为:

addnumber <- 0

for (i in column length){
if (hours(i) > hours(i+1)){
    addnumber <- addnumber + 1
}
date <- date + addnumber

必须有一个更好的方法来处理此问题,我希望以简洁的方式对其进行编码以节省计算成本。谢谢。

1 个答案:

答案 0 :(得分:2)

这是使用import { Component, OnInit } from '@angular/core'; import { of, combineLatest } from 'rxjs'; import { map,toArray,switchMap,tap,distinctUntilKeyChanged } from 'rxjs/operators'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent implements OnInit { observables = [] ob1 = of([ {id: "id1", categories: Array(1), value: "Stadium"}, {id: "id2", categories: Array(2), value: "ball"} ]) ob2 = of([ {id: "id2", categories: Array(2), value: "ball"}, {id: "id4", categories: Array(1), value: "Woman"}, {id: "id5", categories: Array(2), value: "head"} ]) public ngOnInit(): void { this.observables.push(this.ob1); this.observables.push(this.ob2); const latest = combineLatest(...this.observables).pipe(switchMap((arr)=>{ return arr.reduce((acc,curr)=>acc.concat(curr),[]) })); const result = latest.pipe( distinctUntilKeyChanged("id"),toArray() ); result.subscribe(words => { console.warn(words); }) } }

的一种简便方法
dplyr::lag