I\'ve got a dataframe with a text column name
and factor city
. It is ordered alphabetically firstly by city
and then name
. No
In base R using by
:
Set up some test data, including an additional out of range value:
test <- read.table(text="name city
John Atlanta
Josh Atlanta
Matt Atlanta
Bob Boston
Kate Boston
Lily Boston
Matt Boston
Bob Seattle
Kate Seattle",header=TRUE)
Get the 3rd item in each city:
do.call(rbind,by(test,test$city,function(x) x[3,]))
Result:
name city
Atlanta Matt Atlanta
Boston Lily Boston
Seattle
To get exactly what you want, here is a little function:
nthrow <- function(dset,splitvar,n) {
result <- do.call(rbind,by(dset,dset[splitvar],function(x) x[n,]))
result[,splitvar][is.na(result[,splitvar])] <- row.names(result)[is.na(result[,splitvar])]
row.names(result) <- NULL
return(result)
}
Call it like:
nthrow(test,"city",3)
Result:
name city
1 Matt Atlanta
2 Lily Boston
3 Seattle