Home > Blockchain >  How can I use filter() to match the intent of the assignment?
How can I use filter() to match the intent of the assignment?

Time:09-16

My assignment must satisfy the following conditions:

  • Use filter() on airlines to identify which airline corresponds to the carrier code.

  • Save the result to a variable, fastest_airline.

  • Hint: filter(airlines, carrier = ???)

required libraries

library(nycflights13)
library(tidyverse)

I've made new variable 'speed' and sorted it by descending order.

flights_with_speed <- mutate(flights, speed = distance / air_time*60)
flights_with_speed

flights_sort_speed <- arrange(flights_with_speed, desc(speed)) %>%
  select(carrier,speed)
flights_sort_speed

And this part is what i want to ask you about. As the reqirement above i should use filter() on airlines to identify which airline corresponds to the carrier code, and sort it by speed of flights as i made above. But i have no idea solve this problem with filter() function.

fastest_airline <- ???

CodePudding user response:

You need to group_by the carrier and summarize to get the average speed for each carrier. Then you can arrange them in order, and slice the first row to get the fastest carrier along with its average speed:

library(tidyverse)
library(nycflights13)

flights %>% 
  mutate(speed = distance / air_time * 60) %>%
  group_by(carrier) %>%
  summarize(speed = mean(speed, na.rm = TRUE)) %>%
  arrange(-speed) %>%
  slice_head(n = 1)
#> # A tibble: 1 x 2
#>   carrier speed
#>   <chr>   <dbl>
#> 1 HA       480.

Here we see that HA is the fastest carrier on average according to your speed calculation.

Created on 2022-09-15 with reprex v2.0.2

CodePudding user response:

Two ways you could do this without changing the code you already prepped.

  1. Because you've already arranged by decreasing speed, is the answer not just the first observation for carrier, i.e flights_sort_speed$carrier[1]

  2. In order to check the above, and answer using the filter we can also run the below:

flights_sort_speed %>% filter(speed==max(speed,na.rm=TRUE)) %>% select(carrier)

And then check to see if this equals your ordered tibble

flights_sort_speed$carrier[1] == flights_sort_speed %>% filter(speed==max(speed,na.rm=TRUE)) %>% select(carrier)

  1. And if you wanted to do it all in a single command:

flights %>% mutate(speed = distance / air_time*60) %>% filter(speed == max(speed,na.rm=TRUE)) %>% select(carrier)

  •  Tags:  
  • r
  • Related