mutate_impl(.data,dots)出错:评估错误:二元运算符的非数字参数

时间:2017-11-30 13:04:14

标签: r dplyr

当我尝试运行以下功能时:

foo <- air_reserve %>%
  mutate(reserve_date = date(reserve_datetime),
         reserve_hour = hour(reserve_datetime),
         reserve_wday = wday(reserve_datetime, label = TRUE),
         visit_date = date(visit_datetime),
         visit_hour = hour(visit_datetime),
         visit_wday = wday(visit_datetime, label = TRUE),
         diff_hour = time_length(visit_datetime - reserve_datetime, unit = "hour"),
         diff_day = time_length(visit_datetime - reserve_datetime, unit = "day")
  )

我收到以下错误:

Error in mutate_impl(.data, dots) : 
  Evaluation error: non-numeric argument to binary operator.

我该如何解决这个问题?

注意:

foo函数取自此link

1 个答案:

答案 0 :(得分:2)

哦!!

我通过以下代码解决了这个问题:

foo <- air_reserve %>%
  mutate(reserve_date = date(reserve_datetime),
         reserve_hour = hour(reserve_datetime),
         reserve_wday = wday(reserve_datetime, label = TRUE),
         visit_date = date(visit_datetime),
         visit_hour = hour(visit_datetime),
         visit_wday = wday(visit_datetime, label = TRUE), 
         diff_hour = time_length(as.numeric(visit_datetime) - as.numeric(reserve_datetime), unit = "hour"),
         diff_day = time_length(as.numeric(visit_datetime) - as.numeric(reserve_datetime), unit = "day")

  )