Skip to content

Instantly share code, notes, and snippets.

@sudu
Created December 13, 2016 03:38
Show Gist options
  • Save sudu/4a12a874c6d759cb203c4527ebe20535 to your computer and use it in GitHub Desktop.
Save sudu/4a12a874c6d759cb203c4527ebe20535 to your computer and use it in GitHub Desktop.
dplyr - select first and last row from grouped data
# from http://stackoverflow.com/questions/31528981/dplyr-select-first-and-last-row-from-grouped-data
#
# select first and last row from grouped data
# df adjusted for demonstration
df <- data.frame(id=c(1,1,1,2,2,2,3),
stopId=c("a","b","c","a","b","c","a"),
stopSequence=c(1,2,3,3,1,4,3))
# way1
> df %>%
+ group_by(id) %>%
+ arrange(id,stopSequence) %>%
+ filter(row_number()==1 | row_number()==n())
Source: local data frame [5 x 3]
Groups: id [3]
id stopId stopSequence
<dbl> <fctr> <dbl>
1 1 a 1
2 1 c 3
3 2 b 1
4 2 c 4
5 3 a 3
# way2, different from way1, select the row twice if data groups have one row only
> df %>% arrange(id, stopSequence) %>% group_by(id) %>% slice(c(1,n()))
Source: local data frame [6 x 3]
Groups: id [3]
id stopId stopSequence
<dbl> <fctr> <dbl>
1 1 a 1
2 1 c 3
3 2 b 1
4 2 c 4
5 3 a 3
6 3 a 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment