如何找到满足条件的组中的第一个元素 [英] How to find first element of a group that fulfill a condition
本文介绍了如何找到满足条件的组中的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
structure(list(group = c(17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L,
19L, 19L, 20L, 20L, 20L, 21L, 21L, 22L, 23L, 24L, 25L, 25L, 25L,
26L, 27L, 27L, 27L, 28L), var = c(74L, 49L, 1L, 74L, 1L, 49L,
61L, 49L, 1L, 5L, 5L, 1L, 44L, 44L, 12L, 13L, 5L, 5L, 1L, 1L,
4L, 4L, 1L, 1L, 1L, 49L), first = c(0, 0, 1, 0, 1, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)), .Names = c("group",
"var", "first"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-26L))
使用第一个数据两列我想创建第三列(称为 first
),其中仅当<$ c时 first == 1
$ c> var == 1 在组中第一次。换句话说,我想标记组
中的第一个元素,其完全填充 var == 1
。如何在 dplyr
中做到这一点?当然应该使用 group_by
,但是下一步是什么?
With the data from the first two column I would like to create a third column (called first
) where first == 1
only when var == 1
for the first time in a group. In other words I would like to mark first elements within group
that fullfil var == 1
. How can I do that in dplyr
? Certainly group_by
should be used but what next?
推荐答案
用于未分组的数据,一个解决方案是
For ungrouped data, one solution is
first_equal_to = function(x, value)
(x == value) & (cumsum(x == value) == 1)
so
tbl %>% group_by(group) %>% mutate(first = first_equal_to(var, 1))
(将其保留为逻辑向量似乎很合适,因为这就是该列所代表的意思)。
(it seems appropriate to keep this as a logical vector, since that is what the column represents).
另一种实现方式是
first_equal_to2 = function(x, value) {
result = logical(length(x))
result[match(value, x)] = TRUE
result
}
这篇关于如何找到满足条件的组中的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文