Francesco Francesco - 2 months ago 11
R Question

How to select the first and last row within a grouping variable in a data frame?

How can i select the first and last row for each unique

id
in the following dataframe?

id d gr mm area
15 1 2 3.40 1
15 1 1 4.90 2
15 1 1 4.40 1
15 1 1 5.50 2
21 1 1 4.00 2
21 1 2 3.80 2
22 1 1 4.00 2
22 1 1 4.90 2
22 1 2 4.60 2
23 1 1 2.70 2
23 1 1 4.00 2
23 1 2 3.00 2
24 1 1 3.00 2
24 1 1 2.00 3
24 1 1 4.00 2
24 1 2 2.00 3

rcs rcs
Answer

A plyr solution (tmp is your data frame):

R> library("plyr")
R> ddply(tmp, .(id), function(x) x[c(1, nrow(x)), ])
   id d gr  mm area
1  15 1  2 3.4    1
2  15 1  1 5.5    2
3  21 1  1 4.0    2
4  21 1  2 3.8    2
5  22 1  1 4.0    2
6  22 1  2 4.6    2
7  23 1  1 2.7    2
8  23 1  2 3.0    2
9  24 1  1 3.0    2
10 24 1  2 2.0    3