Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter by Date throws error (Error in filter_impl(.data, quo) : Evaluation error: character string is not in a standard unambiguous format.) #4505

Closed
yogat3ch opened this issue Jul 23, 2019 · 3 comments

Comments

@yogat3ch
Copy link

This may be related to #2432 .
Fairly simple operation, filtering a data.frame by a column named via a variable using rlang's !! with Dates/POSIXct causes the following error:
Error in filter_impl(.data, quo) : Evaluation error: character string is not in a standard unambiguous format.

test <- data.frame(time = seq(lubridate::today() - lubridate::days(30), lubridate::today(), by = "day"), n = rnorm(31))
td_nm <- stringr::str_extract(names(test), "^time$|^date$") %>% subset(subset = !is.na(.)) %>% .[1]
test %>% dplyr::select((!!td_nm))
# works fine
test %>% dplyr::filter((!!td_nm) > {lubridate::today() - lubridate::days(7)})
# Error
@cderv
Copy link
Contributor

cderv commented Jul 23, 2019

I think you don't use !! correctly here and the error is expected. !!td_nm result in a string and this won't work in filter. I see too correct use here:

  • Using filter_at() variant as your column name is a string
  • Using sym() on td_nm to create a symbol from a string and the !! on this
    Here examples for those solutions
library(magrittr)
test <- data.frame(time = seq(lubridate::today() - lubridate::days(30), lubridate::today(), by = "day"), n = rnorm(31))
td_nm <- stringr::str_extract(names(test), "^time$|^date$") %>% 
  subset(subset = !is.na(.)) %>% 
  .[1]
# using filter_at variant is useful in this case
test %>% 
  dplyr::filter_at(td_nm, ~ . > {lubridate::today() - lubridate::days(7)})
#>         time          n
#> 1 2019-07-17  1.9963029
#> 2 2019-07-18  0.7997336
#> 3 2019-07-19 -0.7399006
#> 4 2019-07-20  1.7780947
#> 5 2019-07-21  0.7578848
#> 6 2019-07-22 -1.1845520
#> 7 2019-07-23 -0.2126693
# you need sym here
test %>% dplyr::filter(!!dplyr::sym(td_nm) > {lubridate::today() - lubridate::days(7)})
#>         time          n
#> 1 2019-07-17  1.9963029
#> 2 2019-07-18  0.7997336
#> 3 2019-07-19 -0.7399006
#> 4 2019-07-20  1.7780947
#> 5 2019-07-21  0.7578848
#> 6 2019-07-22 -1.1845520
#> 7 2019-07-23 -0.2126693

Created on 2019-07-23 by the reprex package (v0.3.0)

Hope it helps.

@yogat3ch
Copy link
Author

Thanks! I understand what was going wrong now! Much appreciated @cderv

@lock
Copy link

lock bot commented Jan 27, 2020

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants