Obtaining a subset from the data frame cutting off the outliers

Viewed 45

I am working with a dataset (csv file) on the 195 countries' inflation rates for the 1980-2020 period. I'd like to produce chart that describes the distribution of the inflation values across the 1980-2020 period. In the meantime I have produced this chart:

enter image description here

My problem with the chart is that I would like to depict a truncated dataset: with inflation values that are in the 99.5% percentile and the outliers being eliminated from the dataset. Any insight is very much appreciated.

1 Answers

You could use the quantile-function:

library(dplyr)

data %>% 
  group_by(year) %>% 
  filter(value <= quantile(value, 0.995))

removes every value > 99.5 % percentile calculated per year. If you don't want it to be calculated per year, remove the group_by()-line.

Related