How to find top and bottom decile

Hello! I need some help with R Studio and a code.

  1. Find and list the ten country codes that appear most frequently in the top decile for real GDPpc (rgdppc). Find and list the ten country codes that appear most frequently in the bottom decile for real GDPpc (rgdppc).

I am trying this but nothing seems to be working. I'm trying to figure out if I'm in the right direction... I can't find a code that will list the first 10 that come up in the 10% and 90% deciles for 2 variables.

gdp<-PS3$rgdppc %>%. # my object for GDPpc

select(PS3$ccode, gdp) %>% # trying to tell R to select country code + GDPpc

quantile(PS3$ccode, probs = seq(.1, .9, by = .1)) # getting the quantiles

You can wrap your code in backticks (```) to format it better.

gdp<-PS3$rgdppc %>%. # my object for GDPpc
    select(PS3$ccode, gdp) %>% # trying to tell R to select country code + GDPpc
    quantile(PS3$ccode, probs = seq(.1, .9, by = .1)) # getting the quantiles

Without having access to your data, we are limited in how much we can help.
FAQ: What's a reproducible example (reprex) and how do I create one?

Your pipeline does not use correct syntax, however. You are piping a vector (first line) into a function that wants a dataframe (second line) and then into another function that needs a vector (third line).

To take a complete guess, I might do something like this:

library(tidyverse)

PS3 %>%
  select(ccode, gdp) %>%
  filter(gdp >= quantile(gdp, .9, na.rm = T)) %>%
  count(ccode) %>%
  arrange(desc(ccode))
1 Like

Thank you very much for your help. I was able to run this code and worked smoothly.