基于行索引拆分数据框

问题描述 投票:1回答:1

我想通过循环遍历行和子集来分割我的数据框

indices = (diff(Data$Time>1800))
for (i in 1:length(indices)){
if(indices[i]==TRUE){
##### I need a function to split data by row index
}}

我试过了

lst <- split(Data, as.factor(diff(Data$Time>1800)==TRUE))

但它只返回真实的行,但我想要的是每次面对true时分割。

这就是我所拥有的

    Time        temp
7/1/17 13:45:34  56
7/1/17 13:45:37  68
7/1/17 13:45:39  98
7/1/17 13:45:40  99
7/1/17 13:45:46  97
7/1/17 14:16:29  48
7/1/17 14:16:30  78
7/1/17 14:16:31  66
7/1/17 14:17:34  93
7/1/17 14:17:39  98
7/1/17 14:17:40  98
7/1/17 14:17:44  93
7/1/17 14:47:10  54
7/1/17 14:47:12  67
7/1/17 14:47:16  69
7/1/17 14:47:18  95
7/1/17 14:47:19  95
7/1/17 14:47:23  96
7/1/17 14:47:28  96
7/1/17 14:47:30  99

这就是我要的

Time        temp
7/1/17 13:45:34  56
7/1/17 13:45:37  68
7/1/17 13:45:39  98
7/1/17 13:45:40  99
7/1/17 13:45:46  97

Time        temp
7/1/17 14:16:29  48
7/1/17 14:16:30  78
7/1/17 14:16:31  66
7/1/17 14:17:34  93
7/1/17 14:17:39  98
7/1/17 14:17:40  98
7/1/17 14:17:44  93

Time        temp
7/1/17 14:47:10  54
7/1/17 14:47:12  67
7/1/17 14:47:16  69
7/1/17 14:47:18  95
7/1/17 14:47:19  95
7/1/17 14:47:23  96
7/1/17 14:47:28  96
7/1/17 14:47:30  99

是否可以通过将这些索引存储在向量中然后根据此向量拆分数据帧来拆分数据,这意味着只要行号等于我们的值“i”就会在该行拆分数据帧。从而拥有多个数据帧。

r dataframe split subset
1个回答
1
投票

使用新数据集,它似乎不是1800,它应该是1700

library(dplyr)
library(purrr)
library(lubridate)
Data %>% 
    mutate(Time = dmy_hms(Time), 
           grp = cumsum(Time - lag(Time, default = Time[1L])> 1700)) %>%
    split(.$grp) %>% 
    map(~ .x %>%
            select(-grp))
#$`0`
#                 Time temp
#1 2017-01-07 13:45:34   56
#2 2017-01-07 13:45:37   68
#3 2017-01-07 13:45:39   98
#4 2017-01-07 13:45:40   99
#5 2017-01-07 13:45:46   97

#$`1`
#                  Time temp
#6  2017-01-07 14:16:29   48
#7  2017-01-07 14:16:30   78
#8  2017-01-07 14:16:31   66
#9  2017-01-07 14:17:34   93
#10 2017-01-07 14:17:39   98
#11 2017-01-07 14:17:40   98
#12 2017-01-07 14:17:44   93

#$`2`
#                  Time temp
#13 2017-01-07 14:47:10   54
#14 2017-01-07 14:47:12   67
#15 2017-01-07 14:47:16   69
#16 2017-01-07 14:47:18   95
#17 2017-01-07 14:47:19   95
#18 2017-01-07 14:47:23   96
#19 2017-01-07 14:47:28   96
#20 2017-01-07 14:47:30   99

base R相似的选项将是

split(Data, cumsum(c(0, diff(as.POSIXct(Data$Time,
             format = "%d/%m/%y %H:%M:%S", tz = 'GMT'))) > 1700))
© www.soinside.com 2019 - 2024. All rights reserved.