根据阈值分割数据帧

时间:2018-09-22 00:23:30

标签: r datetime split

我需要将此数据帧拆分为多个时滞少于481分钟的数据帧。也就是说,我需要将这些数据块作为单独的数据帧,并消除之间有较长时间延迟的行。我还需要一种为所有这些数据框都具有唯一名称的方法。

        lon      lat              dt          timelag
     -89.80958 38.11485 2017-02-03 06:00:34   359.98333
     -89.80855 38.11436 2017-02-03 12:00:33   119.88333
     -89.80845 38.11474 2017-02-03 14:00:26   480.11667
     -89.80888 38.11307 2017-02-03 22:00:33   839.98333
     -89.80898 38.11240 2017-02-04 12:00:32   240.00000
     -89.80848 38.11260 2017-02-04 16:00:32   119.90000
     -89.79967 38.11637 2017-02-04 18:00:26   119.81667
     -89.80796 38.11332 2017-02-04 20:00:15  1079.93333
     -89.80774 38.11411 2017-02-05 14:00:11   120.06667
     -89.80071 38.11200 2017-02-05 16:00:15   240.26667
     -89.79812 38.11583 2017-02-05 20:00:31   119.70000
     -89.79623 38.11827 2017-02-05 22:00:13   960.28333
     -89.78739 38.11911 2017-02-06 14:00:30   119.78333
     -89.78946 38.11806 2017-02-06 16:00:17   120.21667
     -89.78921 38.11793 2017-02-06 18:00:30   119.86667
     -89.78913 38.11887 2017-02-06 20:00:22   119.76667
     -89.78917 38.11877 2017-02-06 22:00:08   600.15000
     -89.80774 38.11411 2017-02-05 14:00:11   120.06667
     -89.80071 38.11200 2017-02-05 16:00:15   240.26667

生成的数据帧应如下所示:

        lon      lat              dt          timelag
     -89.80958 38.11485 2017-02-03 06:00:34   359.98333
     -89.80855 38.11436 2017-02-03 12:00:33   119.88333
     -89.80845 38.11474 2017-02-03 14:00:26   480.11667

        lon      lat              dt          timelag
     -89.80898 38.11240 2017-02-04 12:00:32   240.00000
     -89.80848 38.11260 2017-02-04 16:00:32   119.90000
     -89.79967 38.11637 2017-02-04 18:00:26   119.81667

        lon      lat              dt          timelag
     -89.80774 38.11411 2017-02-05 14:00:11   120.06667
     -89.80071 38.11200 2017-02-05 16:00:15   240.26667
     -89.79812 38.11583 2017-02-05 20:00:31   119.70000

        lon      lat              dt          timelag
     -89.78739 38.11911 2017-02-06 14:00:30   119.78333
     -89.78946 38.11806 2017-02-06 16:00:17   120.21667
     -89.78921 38.11793 2017-02-06 18:00:30   119.86667
     -89.78913 38.11887 2017-02-06 20:00:22   119.76667

        lon      lat              dt          timelag
     -89.80774 38.11411 2017-02-05 14:00:11   120.06667
     -89.80071 38.11200 2017-02-05 16:00:15   240.26667

谢谢!

1 个答案:

答案 0 :(得分:0)

我为你做的。考虑将数据导入为DF

storage <- vector('list', sum(DF$timelag > 481)+1)

spliter <- which(DF$timelag > 481)

count <- 1
Last <- 1
for (Logical in DF$timelag < 481) {

    if (Logical == FALSE) {
        storage[[count]] <- DF[Last:(spliter[count]-1), ]
        Last <- spliter[count] + 1
        count <- count + 1
    }
}

每个数据框都将放置在列表(storage)索引中