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

ntile behaviour #2564

Closed
MikeBadescu opened this issue Mar 23, 2017 · 1 comment
Closed

ntile behaviour #2564

MikeBadescu opened this issue Mar 23, 2017 · 1 comment

Comments

@MikeBadescu
Copy link

@MikeBadescu MikeBadescu commented Mar 23, 2017

The result of function ntile depends on the the proportion of NAs in argument x. This is because we divide by the length of x, regardless of how many missing values are in row_number(x). Ideally, NAs should be ignored all together.

# this is the dplyr function
# ntile <- function (x, n) {
#     floor((n * (row_number(x) - 1)/length(x)) + 1)
# }

# proposed changes (illustrative)
ntile2 <- function (x, n) {
    
    len <- sum(!is.na(x))
    if (len == 0) return(x)
    floor((n * (dplyr::row_number(x) - 1)/len) + 1)
}

x00 <- numeric()
x04 <- rep(NA_real_, 4)
x40 <- c(1,2,3,4)
x44 <- c(1,2,3,4, rep(NA_real_, 4))
x48 <- c(1,2,3,4, rep(NA_real_, 8))

dplyr::ntile(x00, 0)
#> numeric(0)
dplyr::ntile(x04, 4)
#> [1] NA NA NA NA
dplyr::ntile(x40, 4)
#> [1] 1 2 3 4
dplyr::ntile(x44, 4)
#> [1]  1  1  2  2 NA NA NA NA
dplyr::ntile(x48, 4)
#> [1]  1  1  1  2 NA NA NA NA NA NA NA NA

ntile2(x00, 4)
#> numeric(0)
ntile2(x04, 4)
#> [1] NA NA NA NA
ntile2(x40, 4)
#> [1] 1 2 3 4
ntile2(x44, 4)
#> [1]  1  2  3  4 NA NA NA NA
ntile2(x48, 4)
#> [1]  1  2  3  4 NA NA NA NA NA NA NA NA
@hadley hadley closed this in 611ae3e Mar 23, 2017
@MikeBadescu
Copy link
Author

@MikeBadescu MikeBadescu commented Mar 23, 2017

Thank you!

Loading

@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant