Great than sign and quosure producing error

北城余情 提交于 2019-12-11 07:16:53

问题


I am encountering an issue when using dplyr in a function. When filtering based on a quosure, the > sign appears to cause an issue where no data are returned.

For example:

temp_df <- data.frame(
startdate_c = as.Date(c("2011-08-08", "2007-09-01", "2012-01-01", "2012-10-26", "2012-12-01", 
                "2016-01-01", "2006-06-01", "2009-04-01", "2005-02-09", "2004-08-01")),
enddate_c = as.Date(c("2011-08-14", "2012-09-04", "2014-06-06", "2014-02-28", "2013-04-05",  
              "2016-12-01", "2008-04-18", "2009-08-16", "2006-04-30", "2007-06-02")))

Here's a stripped-down version of the function:

filter_f <- function(df, startdate = NULL, enddate = NULL) {

  if(!missing(startdate)) {
    start_var <- enquo(startdate)
  } else if("startdate_c" %in% names(df)) {
    start_var <- quo(startdate_c)
  } else {
    stop("No valid startdate found")
  }

  if(!missing(enddate)) {
    end_var <- enquo(enddate)
  } else if("enddate_c" %in% names(df)) {
    end_var <- quo(enddate_c)
  } else {
    stop("No valid enddate found")
  }

  df <- df %>%
    filter(!!start_var <= as.Date("2011-12-31") &
             !!end_var >= as.Date("2011-01-01"))

  return(df)
}

Expected output:

  startdate_c  enddate_c
1  2011-08-08 2011-08-14
2  2007-09-01 2012-09-04
3  2012-01-01 2014-06-06
4  2012-10-26 2014-02-28
5  2012-12-01 2013-04-05
6  2016-01-01 2016-12-01

Instead, an empty data frame is returned.

If I tweak the code and name the end date rather than using the quosure, it works:

filter_f2 <- function(df, startdate = NULL, enddate = NULL) {

  if(!missing(startdate)) {
    start_var <- enquo(startdate)
  } else if("startdate_c" %in% names(df)) {
    start_var <- quo(startdate_c)
  } else {
    stop("No valid startdate found")
  }

  if(!missing(enddate)) {
    end_var <- enquo(enddate)
  } else if("enddate_c" %in% names(df)) {
    end_var <- quo(enddate_c)
  } else {
    stop("No valid enddate found")
  }

  df <- df %>%
    filter(!!start_var <= as.Date("2011-12-31") &
             enddate_c >= as.Date("2011-01-01"))

  return(df)
}

The code to create the startdate and enddate quosures is identical, and when I switch the filter so the startdate has the >= sign, then the empty df issue arises again. Is this a bug in dplyr or am I doing something wrong?


回答1:


This is an order of operations problem. The comparison operators (<= and >=) have higher precedence than the ! operator. You need to do

  df <- df %>%
    filter((!!start_var) <= as.Date("2011-12-31") &
             (!!end_var) >= as.Date("2011-01-01"))

For some reason, quosues seem to have negative values? Observe

xvar <- quo(x)
quo(!!xvar <= 0)
# ~TRUE
quo(!!xvar >= 0)
# ~FALSE

Compare to ...

quo((!!xvar) <= 0)
# ~(~x) <= 0
quo((!!xvar) >= 0)
# ~(~x) >= 0

And lastly (for reasons I don't fully understand)

quo(x) <= 4
# [1] TRUE
quo(x) >= 4
# [1] FALSE

which also seems to be related to formulas

(~x) <= 2
# [1] TRUE
(~x) >= 2
# [1] FALSE


来源:https://stackoverflow.com/questions/47062424/great-than-sign-and-quosure-producing-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!