0

could anyone please explain to me the function of this code? and what it is intenteded to do?

airports %>%
  semi_join(flights, c("faa" = "dest")) %>%
  ggplot(aes(lon, lat))+
  geom_point()
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Lilguyuno
  • 5
  • 4

1 Answers1

0

If you get confused by a piece of code, break it down in smaller steps and see what each line does. If you are not aware of certain functions, it's documentation is available at ?function_name (example - ?semi_join).

It seems that the datasets airports and flights are from package nycflights13. Load the libraries that are required for running the code.

library(dplyr)
library(nycflights13)
library(ggplot2)

semi_join keeps the common rows that match between the two datasets. The column to match on which the datasets are joined is faa in airports and dest in flights. If you don't know how join works take a smaller example and try this code on it. You may also take a look at How to join (merge) data frames (inner, outer, left, right) for a general explanation of different join functions.

airports %>% semi_join(flights, c("faa" = "dest"))

#   faa   name                               lat    lon   alt    tz dst   tzone            
#   <chr> <chr>                             <dbl>  <dbl> <dbl> <dbl> <chr> <chr>            
# 1 ABQ   Albuquerque International Sunport  35.0 -107.   5355    -7 A     America/Denver   
# 2 ACK   Nantucket Mem                      41.3  -70.1    48    -5 A     America/New_York 
# 3 ALB   Albany Intl                        42.7  -73.8   285    -5 A     America/New_York 
# 4 ANC   Ted Stevens Anchorage Intl         61.2 -150.    152    -9 A     America/Anchorage
# 5 ATL   Hartsfield Jackson Atlanta Intl    33.6  -84.4  1026    -5 A     America/New_York 
# 6 AUS   Austin Bergstrom Intl              30.2  -97.7   542    -6 A     America/Chicago  
# 7 AVL   Asheville Regional Airport         35.4  -82.5  2165    -5 A     America/New_York 
# 8 BDL   Bradley Intl                       41.9  -72.7   173    -5 A     America/New_York 
# 9 BGR   Bangor Intl                        44.8  -68.8   192    -5 A     America/New_York 
#10 BHM   Birmingham Intl                    33.6  -86.8   644    -6 A     America/Chicago  
# … with 91 more rows

After running the code, the above dataframe is plotted with ggplot2 library where x-axis is lon and y-axis is lat. It uses geom_point to plot as scatterplot.

airports %>%
  semi_join(flights, c("faa" = "dest")) %>%
  ggplot(aes(lon, lat))+
  geom_point()

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213