大于符号和定额产生错误 [英] Great than sign and quosure producing error
问题描述
在函数中使用dplyr时遇到问题。当基于一定数量进行过滤时,>符号似乎会导致不返回任何数据的问题。
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.
例如:
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)
}
预期输出:
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.
如果我调整代码并命名结尾日期而不是使用quasure,它起作用:
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)
}
创建开始日期和结束日期的代码是相同的,当我切换过滤器时,开始日期使用> =号,则出现了空df问题再次。这是dplyr中的错误还是我做错了什么?
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?
推荐答案
这是一个操作顺序问题。比较运算符(< =
和> =
)的优先级高于!
运算符。您需要
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
比较...
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
似乎也与公式有关
(~x) <= 2
# [1] TRUE
(~x) >= 2
# [1] FALSE
这篇关于大于符号和定额产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!