I\'ve got a dataframe with a text column name
and factor city
. It is ordered alphabetically firstly by city
and then name
. No
You can use plyr
for this:
dat <- structure(list(name = c("John", "Josh", "Matt", "Bob", "Kate",
"Lily", "Matt"), city = c("Atlanta", "Atlanta", "Atlanta", "Boston", "Boston", "Boston", "Boston")), .Names = c("name", "city"), class = "data.frame", row.names = c(NA, -7L))
library(plyr)
ddply(dat, .(city), function(x, n) x[n,], n=3)
> ddply(dat, .(city), function(x, n) x[n,], n=3)
name city
1 Matt Atlanta
2 Lily Boston
> ddply(dat, .(city), function(x, n) x[n,], n=4)
name city
1
2 Matt Boston
>
There are plenty of other options too using base R or data.table
or sqldf
...