Error finding object when removing outliers in pipe in R

Viewed 25

Okay. I have looked everywhere and read documentation, watched videos, talked to people for help, etc... and cant seem to get this figured out. I need to remove the outliers in one variable of a data set using object assignment and the quartile method, but I have to do it in the pipe. When I run the code, the object cannot be found. Here is the code:

suppressPackageStartupMessages(library(tidyverse))

suppressPackageStartupMessages(library(nycflights13))

suppressPackageStartupMessages(library(lm.beta))

Q1 <- flights %>%
  dep_delay_upper <- quantile(dep_delay$y, 0.997, na.rm = TRUE) %>%
  dep_delay_lower <- quantile(dep_delay$y, 0.003, na.rm = TRUE) %>%
  dep_delay_out <- which(dep_delay$y > dep_delay_upper | dep_delay$y < dep_delay_lower) %>%
  dep_delay_noout <- dep_delay[-dep_delay_out,]

Here is a screenshot with my error in the terminal:

enter image description here

1 Answers

With magrittr's pipe, you can reuse the piped object with a . as so.

The first way gets only the values of dep_delay:

flights$dep_delay %>%
  .[which(. < quantile(., 0.997, na.rm = TRUE) & . > quantile(., 0.003, na.rm = TRUE))]

And the second way filters the entire flights dataframe:

flights %>%
  .[which(.$dep_delay < quantile(.$dep_delay, 0.997, na.rm = TRUE) &
            .$dep_delay > quantile(.$dep_delay, 0.003, na.rm = TRUE)),]

# # A tibble: 326,164 × 19
#     year month   day dep_time sched_dep_time dep_delay arr_time sched_…¹ arr_d…² carrier flight tailnum origin dest  air_t…³ dista…⁴  hour minute time_hour          
#    <int> <int> <int>    <int>          <int>     <dbl>    <int>    <int>   <dbl> <chr>    <int> <chr>   <chr>  <chr>   <dbl>   <dbl> <dbl>  <dbl> <dttm>             
#  1  2013     1     1      517            515         2      830      819      11 UA        1545 N14228  EWR    IAH       227    1400     5     15 2013-01-01 05:00:00
#  2  2013     1     1      533            529         4      850      830      20 UA        1714 N24211  LGA    IAH       227    1416     5     29 2013-01-01 05:00:00
#  3  2013     1     1      542            540         2      923      850      33 AA        1141 N619AA  JFK    MIA       160    1089     5     40 2013-01-01 05:00:00
#  4  2013     1     1      544            545        -1     1004     1022     -18 B6         725 N804JB  JFK    BQN       183    1576     5     45 2013-01-01 05:00:00
#  5  2013     1     1      554            600        -6      812      837     -25 DL         461 N668DN  LGA    ATL       116     762     6      0 2013-01-01 06:00:00
#  6  2013     1     1      554            558        -4      740      728      12 UA        1696 N39463  EWR    ORD       150     719     5     58 2013-01-01 05:00:00
#  7  2013     1     1      555            600        -5      913      854      19 B6         507 N516JB  EWR    FLL       158    1065     6      0 2013-01-01 06:00:00
#  8  2013     1     1      557            600        -3      709      723     -14 EV        5708 N829AS  LGA    IAD        53     229     6      0 2013-01-01 06:00:00
#  9  2013     1     1      557            600        -3      838      846      -8 B6          79 N593JB  JFK    MCO       140     944     6      0 2013-01-01 06:00:00
# 10  2013     1     1      558            600        -2      753      745       8 AA         301 N3ALAA  LGA    ORD       138     733     6      0 2013-01-01 06:00:00
# # … with 326,154 more rows, and abbreviated variable names ¹​sched_arr_time, ²​arr_delay, ³​air_time, ⁴​distance
# # ℹ Use `print(n = ...)` to see more rows

Or alternatively with dplyr:

flights %>%
  filter(dep_delay < quantile(dep_delay, 0.997, na.rm = TRUE) &
           dep_delay > quantile(dep_delay, 0.003, na.rm = TRUE))
Related