Skip to content

Weird behavior of group_by + lag #925

@tongcx

Description

@tongcx
library(dplyr)
df = read.csv(text ='
   name time
1   Rob    3
2  Pete    2
3   Rob    5
4  John    3
5   Rob    2
6  Pete    3
7  John    2
8  Pete    4
9  John    1
10 Pete    1
11  Rob    4
12  Rob    1
', sep='')
df %>% group_by(name) %>% mutate(lag_time = lag(time))

Gets me

   name time lag_time
1   Rob    3       NA
2  Pete    2       NA
3   Rob    5        2
4  John    3       NA
5   Rob    2        3
6  Pete    3        2
7  John    2        3
8  Pete    4        2
9  John    1        4
10 Pete    1        1
11  Rob    4        1
12  Rob    1        4

Should row 3 be Rob 5 3? Is this a bug or intended behavior?

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions