Search code examples
routliersfacebook-prophet

Removing outliers by filtering values in R


i have a dataframe like this :

         ds        y
1   2015-12-31 35.59050
2   2016-01-01 28.75111
3   2016-01-04 25.53158
4   2016-01-06 17.75369
5   2016-01-07 29.01500
6   2016-01-08 29.22663
7   2016-01-09 29.05249
8   2016-01-10 27.54387
9   2016-01-11 28.05674
10  2016-01-12 29.00901
11  2016-01-13 31.66441
12  2016-01-14 29.18520
13  2016-01-15 29.79364
14  2016-01-16 30.07852

i'm trying to create a loop that remove the rows which values in the 'ds' column are above 34 or below 26, because there is where my outliers are:

for (i in grupo$y){if (i < 26) {grupo$y[i] = NA}}

i tried this to remove those below 26, i don't get any errors, but those rows won't go.

Any suggestions about how to remove those outliers??

Thanks in advance


Solution

  • With dplyr you could do:

    library(dplyr)
    df %>% 
    filter(y >= 26 & y <= 34)
    
           ds        y
    1  2016-01-01 28.75111
    2  2016-01-07 29.01500
    3  2016-01-08 29.22663
    4  2016-01-09 29.05249
    5  2016-01-10 27.54387
    6  2016-01-11 28.05674
    7  2016-01-12 29.00901
    8  2016-01-13 31.66441
    9  2016-01-14 29.18520
    10 2016-01-15 29.79364
    11 2016-01-16 30.07852