Q:

R how to get the third and last row for each user

I have a table that has information about certain users. What I'm trying to do is extract the third and last row of each user...How would I do that? So for example:
User | num |

use1 | 3 |

use1 | 2 |

use1 | 5 |

use1 | 8 |

use1 | 6 |

use2 | 1 |

..

So in this example, I'd want the row with num = 5 and num = 6 and this would continue for each different user in the table.

A:

For variety, here is a plyr solution:
library(plyr)

#group by user and return 3rd and last row
ddply(df, .(User), function(x) x[c(3, nrow(x)),])

