Problem with map2 function

I'm trying to create a function that captures the amplitude of flight arrivals and departures. but getting this error: "non-numeric argument to binary operato". even though through the command class I discovered that arr and dep are numeric. I'm using nyclights13 data. Can you guys help?


data <- flights %>% 
  group_by(dep_delay, arr_delay) %>% 
  summarise(n = n())

deptime_delay <- data$dep_delay


arrtime_delay <- data$arr_delay


fmp2 <- function(x, y) {
  result <- max("deptime_delay:", x, "arrtime_delay:", y) - min("deptime_delay:", x, "arrtime_delay:", y)

map2(deptime_delay, arrtime_delay , fmp2) %>% head(5)

Hi @Shin1 ,

Really cool idea!

I'd like to take a stab at helping you out here, but let me know if I misunderstand your function fmp2.

What I believe is throwing the non-numeric to binary operator error is the presence of "deptime_delay:" and the other strings in the max() and min() functions. Now I could be wrong with that but I think those functions are only capable of handling numerics. I'm not sure what your goal is with those strings, whether you're looking to label your result or specify a column of data.

Here is a version of your code, minus the strings in the max and min functions:

# two example lists of numeric values to use
test_list_1 <- c(1, 2, 3, 4, 65)
test_list_2 <- c(12, 20, 100, 10, 9)

# version of your function that removes the character strings
test_fxn <- function(x, y) {
  testResult <- max(x, y) - min(x, y)

# running map2 over the lists with the test function
map2(test_list_1, test_list_2, test_fxn)

I hope this helps!

Hi @Shin1 ,

Any luck getting your function to work?

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.