I'm trying to get the top three observation in the asset variable for each state. Using a smaller example, my starting data frame would be as follows:
#starting data frame
state = c("TX","TX","TX","TX","TX","TX","PA","PA","PA","PA","PA","PA","DE","DE","DE","DE","DE","DE")
assets = c(600,100,200,400,200,300,600,100,200,400,200,300,600,100,200,400,200,300)
stating_df = data.frame(state,assets)
And I'd like to go to this:
#ending data frame
state = c("TX","TX","TX","PA","PA","PA","DE","DE","DE")
assets = c(600,500,400,600,500,400,600,500,400)
ending_df = data.frame(state,assets)
I can easily use the sort
command, but that doesn't account for each state or the top three. I have 50 states and over 1m observations in the data frame. Thank you.