[R] by group problem

Erich Neuwirth
Sat, 01 Sep 2007 09:16:27 -0000

Perhaps you want this?

data <- NULL
data$state <- c(rep("Illinois", 10), rep("Wisconsin",
10))
data$county <- c("Adams", "Brown", "Bureau",
"Cass", "Champaign",
                 "Christian", "Coles", "De Witt",
"Douglas", "Edgar",
                 "Adams", "Ashland", "Barron",
"Bayfield", "Buffalo",
                 "Burnett", "Chippewa", "Clark",
"Columbia", "Crawford")
data$percentOld <- c(17.554849, 16.826594, 18.196593, 17.139242,  8.743823,
                     17.862746, 13.747967, 16.626302, 15.258940, 18.984435,
                     19.347022, 17.814436, 16.903067, 17.632781, 16.659305,
                     20.337817, 14.293354, 17.252820, 15.647179, 16.825596)

data<-data.frame(data,stringsAsFactors=FALSE)
rankWithinState<-unlist(tapply(-data$percentOld,data$state,rank))
names(rankWithinState)<-NULL
data<-data.frame(data,rankWithinState)
highCounties<-data[data$rankWithinState<=5,]
highCountiesSorted<-highCounties[order(highCounties$state,-highCounties$percentOld),]

Cory Nissen wrote:
> I am working with census data.  My columns of interest are...
>
> PercentOld - the percentage of people in each county that are over 65
> County - the county in each state
> State - the state in the US
--
Erich Neuwirth, University of Vienna
Faculty of Computer Science
Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-39464 Fax: +43-1-4277-39459