I have data with discrete x values such as
x = c(3,8,13,8,13,3,3,8,13,8,3,8,8,13,8,13,8,3,3,8,13,8,13,3,3)
y = c(4,5,4,6,7,20,1,4,6,2,6,8,2,6,7,3,2,5,7,3,2,5,7,3,2);
How can I create a new dataset of x and y values, where I exclude pairs of values, where the y-value is 2 standard deviations above the average for this bin. For example, in a buffer x = 3, 20 is more than 2 SD above average, so the data point must be deleted.