[r] Select first and last row from grouped data