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

roll_any() and roll_all() #14

Closed
hadley opened this issue Sep 23, 2018 · 4 comments
Closed

roll_any() and roll_all() #14

hadley opened this issue Sep 23, 2018 · 4 comments

Comments

@hadley
Copy link

hadley commented Sep 23, 2018

Would you consider extending roll to also think about logical vectors? roll_any() and roll_all() are admittedly esoteric, but can sometimes be useful, and would complete the matrix of summary functions at https://github.com/r-lib/vctrs/issues/9

@jasonjfoster
Copy link
Owner

Thanks, that's an interesting suggestion. Just so I understand what you’re thinking, here’s an example of how I’d approach it in base R:

library(roll)
library(xts)

n_vars <- 3
n_obs <- 15
x <- matrix(sample(c(TRUE, FALSE), (n_obs * n_vars), replace = TRUE),
            nrow = n_obs, ncol = n_vars)

rollapplyr(x, 5, any)
rollapplyr(x, 5, all)

If so, I can see it fitting within my framework and existing arguments: min_obs, complete_obs, na_restore. An exception is the weights argument but that wouldn't make sense with a logical matrix anyway. Will look into it further.

@hadley
Copy link
Author

hadley commented Sep 25, 2018

Yeah, exactly.

@jasonjfoster
Copy link
Owner

I have added both roll_any and roll_all in the development version. Here's an example:

# devtools::install_github("jjf234/roll")

library(roll)
library(xts)
library(testthat)

n_vars <- 30
n_obs <- 1500
x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars)

# rolling any ('min_obs = 1' equivalent to 'partial = TRUE')
expect_equivalent(roll_any(x < 0, 5, min_obs = 1),
                  rollapplyr(x < 0, 5, any, partial = TRUE))

# rolling all
expect_equivalent(roll_all(x < 0, 5, min_obs = 1),
                  rollapplyr(x < 0, 5, all, partial = TRUE))

Also, the performance is significantly faster. Let me know if you have any other feedback.

@hadley
Copy link
Author

hadley commented Oct 17, 2018

Looks great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants