Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first occurence of value in group using dplyr mutate

Tags:

r

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.

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)) 

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
like image 211
Rasmus Larsen Avatar asked Dec 04 '22 04:12

Rasmus Larsen


1 Answers

You may use !duplicated.

mtcars %>%
  select(cyl, carb) %>%
  group_by(cyl) %>%
  mutate(first_4 = carb == 4 & !duplicated(carb == 4))  %>%
  arrange(cyl)
like image 119
Henrik Avatar answered Jan 11 '23 22:01

Henrik