如何标记r中列中两个特定字符值之间的所有行?

时间:2017-08-10 17:12:44

标签: r

问题描述

August 7th 2017, 00; SumOfMaxCurrentPoints -> 27 (max from hour 00h from both users 10 + 17) August 7th 2017, 00; SumOfMaxCurrentPoints -> 15 (max from hour 01h from both users 4 + 11) 列(下方)中的startend指的是汽车换道的开始和结束。对于驱动程序的每个frame_type,我想标记从开始到结束的所有行为" LC"在一个新专栏中。

数据

id

期望输出:

foo <- data.frame(id = c(rep(1, 20), rep(2, 10)),
                  frame_type = rep(c(".", ".", ".", 
                                     "start", ".", "lcf", ".",
                                     ".", "end", "."), 3))
> foo
   id frame_type
1   1          .
2   1          .
3   1          .
4   1      start
5   1          .
6   1        lcf
7   1          .
8   1          .
9   1        end
10  1          .
11  1          .
12  1          .
13  1          .
14  1      start
15  1          .
16  1        lcf
17  1          .
18  1          .
19  1        end
20  1          .
21  2          .
22  2          .
23  2          .
24  2      start
25  2          .
26  2        lcf
27  2          .
28  2          .
29  2        end
30  2          .

我搜索了很多,但无法解决这个问题。我所知道的最接近的是> foo id frame_type LC 1 1 . . 2 1 . . 3 1 . . 4 1 start LC1 5 1 . LC1 6 1 lcf LC1 7 1 . LC1 8 1 . LC1 9 1 end LC1 10 1 . . 11 1 . . 12 1 . . 13 1 . . 14 1 start LC2 15 1 . LC2 16 1 lcf LC2 17 1 . LC2 18 1 . LC2 19 1 end LC2 20 1 . . 21 2 . . 22 2 . . 23 2 . . 24 2 start LC1 25 2 . LC1 26 2 lcf LC1 27 2 . LC1 28 2 . LC1 29 2 end LC1 30 2 . . ,但在这种情况下并不起作用。我想使用tidyr::fill(),因为有几个dplyr::group_by()。请帮忙。

2 个答案:

答案 0 :(得分:3)

我们可以使用describe('Saving records', function() { it('Saves record to db', function() { var ppball = new PPB ({ amount: 5 }); return ppball.save().then(function(){ assert(ppball.isNew === false); return null; }); }); }); 。转换&#39; data.frame&#39;到&#39; data.table&#39; (data.table),按逻辑向量(setDT(foo)),frame_type == "start" if&#39; frame_type&#39;的累积总和进行分组开始&#39;开始&#39;字符串,然后从&#39;开始&#39;获取位置序列的行索引(any)。要结束&#39;,提取该列(.I),将其用作$V1来创建新列&#39; LC&#39;通过i字符串paste,将逻辑索引的累计和按“&id”标记分组。如果需要,"LC"值可以更改为NA(不推荐)

.

答案 1 :(得分:1)

do.call(rbind, lapply(split(foo, foo$id), function(a){
    temp = inverse.rle(with(rle(cumsum(a$frame_type == "start") -
                                    cumsum(head(c(FALSE, a$frame_type == "end"), -1))),
                            list(lengths = lengths,
                                 values = replace(values, values == 1,
                                                  seq_along(values[values == 1])))))
    a$LC = replace(paste0("LC", temp), temp == 0, ".")
    a
}))
#     id frame_type  LC
#1.1   1          .   .
#1.2   1          .   .
#1.3   1          .   .
#1.4   1      start LC1
#1.5   1          . LC1
#1.6   1        lcf LC1
#1.7   1          . LC1
#1.8   1          . LC1
#1.9   1        end LC1
#1.10  1          .   .
#1.11  1          .   .
#1.12  1          .   .
#1.13  1          .   .
#1.14  1      start LC2
#1.15  1          . LC2
#1.16  1        lcf LC2
#1.17  1          . LC2
#1.18  1          . LC2
#1.19  1        end LC2
#1.20  1          .   .
#2.21  2          .   .
#2.22  2          .   .
#2.23  2          .   .
#2.24  2      start LC1
#2.25  2          . LC1
#2.26  2        lcf LC1
#2.27  2          . LC1
#2.28  2          . LC1
#2.29  2        end LC1
#2.30  2          .   .