I have a dataframe called df
:
City,State,Price,Dogs
Portland,OR,75,1
Portland,OR,100,3
San Diego,CA,12,4
San Diego,CA,23,5
...
I used dplyr
's summarise
and group_by
functions...
df.median <- summarise(
group_by(
df,
State,
City
),
MEDIAN_PRICE = median(Price),
SUM_DOGS = sum(Dogs)
)
But when I run top_n(df.median, 100, SUM_DOGS)
, R does not give me cities with the 100 highest values in SUM_DOGS
. It just returns df.median
.
Why?
You likely need to ungroup
, so you pick the top_n
from the whole dataset rather than the top_n
from each State (as your dataset is currently grouped).
top_n(ungroup(df.median), 100, SUM_DOGS)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With