DJF
DJF

Reputation: 103

subsetting rows in R for two max values

I'm trying to find the rows for the two states with the highest execution rate. I was able to find the first one (Texas row), with the following command:

maxstate = subset(mrate, exec == max(mrate$exec))

So I have the first, but need to figure out how to return the rows with the top two values for exec. mrate is the dataset.
Any help is appreciated.

Upvotes: 2

Views: 59

Answers (2)

dario
dario

Reputation: 6485

with base R you could do something like:

mrate[order(-mreate$exec), ][1:2, ]

Upvotes: 3

akrun
akrun

Reputation: 886948

We can order based on the 'exec' and get the first two or last two rows

head(mrate[order(-mrate$exec),], 2)

Upvotes: 4

Related Questions