使用dplyr mutate查找值在组中的首次出现 [英] Find first occurence of value in group using dplyr mutate

查看:80
本文介绍了使用dplyr mutate查找值在组中的首次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用 dplyr 的组中找到某个值的首次出现。

How do i find the first occurence of a certain value, within a group using dplyr.

以下代码给出了预期的结果,但是我想知道是否有更短的方法来做到这一点。

The following code gives the desired result, but it I'm wondering if there is a shorter way to do it.

此外,我担心 group_by 变异 ,或者其他函数可能会隐式地重新排列行,不知道这是否可能是一个问题?

Also, I am worried that group_by or mutate, or some other function might do implicit rearrangement of the rows, don't know if this could be an issue?

mtcars   %>% select( cyl, carb) %>% group_by( cyl ) %>%

   mutate( "occurence_of_4" =  carb == 4 )  %>%

   dplyr::arrange( cyl )  %>%

   group_by( cyl, occurence_of_4)  %>%

   mutate( "count" = 1:n(),
           "first_4_in_cyl_group"  = ifelse( occurence_of_4==TRUE & count==1, TRUE, FALSE)) 

变量first_4_in_cyl_group为 TRUE 对于每个气缸组中首次出现的 4, FALSE 否则:

The variable first_4_in_cyl_group is TRUE for the first occurence of "4" in each cylinder group, FALSE otherwise:

Source: local data frame [32 x 5]
Groups: cyl, occurence_of_4

   cyl carb occurence_of_4 count first_4_in_cyl_group
1    4    1          FALSE     1                FALSE
2    4    2          FALSE     2                FALSE
3    4    2          FALSE     3                FALSE
4    4    1          FALSE     4                FALSE
5    4    2          FALSE     5                FALSE
6    4    1          FALSE     6                FALSE
7    4    1          FALSE     7                FALSE
8    4    1          FALSE     8                FALSE
9    4    2          FALSE     9                FALSE
10   4    2          FALSE    10                FALSE
11   4    2          FALSE    11                FALSE
12   6    4           TRUE     1                 TRUE
13   6    4           TRUE     2                FALSE
14   6    1          FALSE     1                FALSE
15   6    1          FALSE     2                FALSE
16   6    4           TRUE     3                FALSE
17   6    4           TRUE     4                FALSE
18   6    6          FALSE     3                FALSE
19   8    2          FALSE     1                FALSE
20   8    4           TRUE     1                 TRUE
21   8    3          FALSE     2                FALSE
22   8    3          FALSE     3                FALSE
23   8    3          FALSE     4                FALSE
24   8    4           TRUE     2                FALSE
25   8    4           TRUE     3                FALSE
26   8    4           TRUE     4                FALSE
27   8    2          FALSE     5                FALSE
28   8    2          FALSE     6                FALSE
29   8    4           TRUE     5                FALSE
30   8    2          FALSE     7                FALSE
31   8    4           TRUE     6                FALSE
32   8    8          FALSE     8                FALSE


推荐答案

一些修改:


  1. 通过在 mutate 步骤> group_by

  2. ifelse 不需要,因为输出为 TRUE / FALSE

  1. Remove the first mutate step by creating the "occurence_of_4" variable within the group_by
  2. ifelse is not needed as the output will be "TRUE/FALSE"

library(dplyr)
mtcars %>%
     select(cyl, carb) %>%
     group_by(cyl, occurence_of_4= carb==4) %>% 
     arrange(cyl) %>%
     mutate(count= row_number(), 
         first_4_in_cyl_group = occurence_of_4 & count==1)


这篇关于使用dplyr mutate查找值在组中的首次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆