r - Group By then Aggregate -
i working several large data frames , need sort data first , last entry boat , net. data frame looks this:
boat net datetime dawn 71 2014-07-10 10:10 dawn 71 2014-07-15 11:10 whip 71 2014-07-17 08:10 whip 71 2014-07-29 12:36 dawn 71 2014-08-24 14:53 whip 71 2014-09-02 11:17 whip 73 2014-09-14 16:24 whip 71 2014-09-15 18:16 whip 73 2014-09-17 20:25
i need dataframe include first , last entry each net boat. data should looks this:
boat net datetime dawn 71 2014-07-10 10:10 whip 71 2014-07-17 08:10 dawn 71 2014-08-24 14:53 whip 73 2014-09-14 16:24 whip 71 2014-09-15 18:16 whip 73 2014-09-17 20:25
i tried couple of different things , got close not quite there.
head <- aggregate(df, = list(df$net), fun = head, n = 1) tail <- aggregate(df, = list(df$net), fun = tail, n = 1) final <- rbind(head, tail)
this worked not take account same net number on different boats, tried group boat got same result:
head <- df %>% group_by(boat) %>% aggregate(df, = list(df$net), fun = head, n = 1) %>% ungroup
both of these functions returned following data: (the first , last entry net number only)
boat net datetime dawn 71 2014-07-10 10:10 whip 73 2014-09-14 16:24 whip 71 2014-09-15 18:16 whip 73 2014-09-17 20:25
i think close can't quite there, appreciated.
for aggregate approach, can want providing both df$boat
, df$net
aggregate
:
head <- aggregate(df, = list(df$boat, df$net), fun = head, n = 1) tail <- aggregate(df, = list(df$boat, df$net), fun = tail, n = 1) final <- rbind(head, tail)
since try using dplyr's group_by
, here's dplyr alternative, uses slice
group:
final <- df %>% group_by(boat, net) %>% slice(c(1, n())) %>% ungroup()
(note group_by
, aggregate
don't special in combination- group_by
works other dplyr functions slice
, summarize
, or mutate
).
Comments
Post a Comment