Error in Function: Unused Argument?

Hi,

I am trying to create a function which calculates the number of flights in any given time period. Im trying to pass a date through this, to do one date at a time but I am getting the error: Error in getdat(i) : unused argument (i).

Any ideas would be appreciated!

date_list <- seq.POSIXt(as.POSIXct("2020-01-01 00:00:00", tz = "UTC"), 
                        as.POSIXct("2020-01-04 00:00:00", tz = "UTC"), "day")
date_list = data.frame(Date = c(date_list))

#
depcdm <- tibble::tribble(
            ~FlightID,      ~PushbackTime,      ~AirborneTime,
               "XX24", "01/01/2020 22:22", "01/01/2020 22:31",
               "XX35", "01/01/2020 22:27", "01/01/2020 22:46",
               "XX74", "01/01/2020 22:16", "01/01/2020 22:27",
              "XX189", "01/01/2020 22:51", "01/01/2020 23:07",
              "XX179", "01/01/2020 22:21", "01/01/2020 22:33",
               "XX50", "02/01/2020 22:15", "02/01/2020 22:32",
               "XX86", "02/01/2020 22:30", "02/01/2020 22:47",
              "XX104", "02/01/2020 22:31", "02/01/2020 22:50",
              "XX165", "02/01/2020 22:39", "02/01/2020 22:56",
               "XX47", "02/01/2020 23:50", "03/01/2020 00:12",
               "XX63", "03/01/2020 22:31", "03/01/2020 22:40",
              "XX136", "03/01/2020 22:29", "03/01/2020 22:43",
              "XX181", "03/01/2020 22:16", "03/01/2020 22:31",
              "XX168", "03/01/2020 22:16", "03/01/2020 22:36",
              "XX160", "03/01/2020 22:49", "03/01/2020 23:33"
            )
depcdm$Date <- as.Date(depcdm$ScheduledTime)

##
getdat <- function() {
  
  input <- filter(depcdm, depcdm$Date >= min(date_list$Date) & depcdm$Date < max(date_list$Date))
  input$PushbackTime <- lubridate::ymd_hms(input$PushbackTime)
  input$AirborneTime <- lubridate::ymd_hms(input$AirborneTime)
  
  input <- data.frame(DateTime = seq.POSIXt(min(input$PushbackTime),
                                            max(input$AirborneTime), "mins")) %>%
    fuzzy_left_join(input, by = c("DateTime" = "PushbackTime",
                              "DateTime" = "AirborneTime"),
                    match_fun = c(`>=`, `<=`)) %>%
    group_by(DateTime) %>%
    summarise(Movements = n()
    
  )
  
  return(input)

Hello,

I have a few questions / remarks on your code:

  • It seems that your data frame depcdm does not have a column ScheduledTime thus the following line of code produces NULL
    depcdm$Date <- as.Date(depcdm$ScheduledTime)
  • Why are you making a function if you are not passing it any arguments? Either you only need to run this code once and thus you don't need to bother writing a function, or you provide it with the arguments so it can then use different data to produce different output. Something like this:
getdat <- function(flightData, dateList) {
  
  input <- filter(flightData, flightData$Date >= min(dateList$Date) & flightData$Date < max(dateList$Date))
  input$PushbackTime <- lubridate::ymd_hms(input$pushbackTime)
  input$AirborneTime <- lubridate::ymd_hms(input$airborneTime)
  
  input <- data.frame(DateTime = seq.POSIXt(min(input$pushbackTime),
                                            max(input$airborneTime), "mins")) %>%
    fuzzy_left_join(input, by = c("DateTime" = "PushbackTime",
                                  "DateTime" = "AirborneTime"),
                    match_fun = c(`>=`, `<=`)) %>%
    group_by(DateTime) %>%
    summarise(Movements = n()
              
    )
  
  return(input)
}

getdat(depcdm, date_list)

PJ

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.