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

More Efficient group_by(...) %>% sample_*(...) #3193

Merged
merged 2 commits into from Dec 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -45,6 +45,8 @@

* Add error for `distinct()` if any of the selected columns are of type `list` (#3088, @foo-bar-baz-qux).

* `sample_n()` and `sample_frac()` on grouped data frame are now faster especially for those with large number of groups (#3193, @saurfang).

* Better error message if dbplyr is not installed when accessing database backends (#3225).

* Fix `row_number()` and `ntile()` ordering to use the locale-dependent ordering functions in R when dealing with character vectors, rather than always using the C-locale ordering function in C (#2792, @foo-bar-baz-qux).
Expand Down
9 changes: 4 additions & 5 deletions R/grouped-df.r
Expand Up @@ -265,11 +265,11 @@ sample_n.grouped_df <- function(tbl, size, replace = FALSE,
inform("`.env` is deprecated and no longer has any effect")
}
weight <- enquo(weight)
weight <- mutate(tbl, w = !!weight)[["w"]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a space after the !! please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, !! is going to have very high precedence so now it makes sense to have it close to its argument just like unary -.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Let me know if there is anything else I can help to help this PR merged.


index <- attr(tbl, "indices")
sampled <- lapply(index, sample_group,
frac = FALSE,
tbl = tbl,
size = size,
replace = replace,
weight = weight
Expand All @@ -292,11 +292,11 @@ sample_frac.grouped_df <- function(tbl, size = 1, replace = FALSE,
)
}
weight <- enquo(weight)
weight <- mutate(tbl, w = !!weight)[["w"]]

index <- attr(tbl, "indices")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks a bit cleaner (and maybe faster) if we assign index <- attr(...) + 1L here and don't add in sample_group().

sampled <- lapply(index, sample_group,
frac = TRUE,
tbl = tbl,
size = size,
replace = replace,
weight = weight
Expand All @@ -306,7 +306,7 @@ sample_frac.grouped_df <- function(tbl, size = 1, replace = FALSE,
grouped_df(tbl[idx, , drop = FALSE], vars = groups(tbl))
}

sample_group <- function(tbl, i, frac, size, replace, weight) {
sample_group <- function(i, frac, size, replace, weight) {
n <- length(i)
if (frac) {
check_frac(size, replace)
Expand All @@ -315,9 +315,8 @@ sample_group <- function(tbl, i, frac, size, replace, weight) {
check_size(size, n, replace)
}

weight <- eval_tidy(weight, tbl[i + 1, , drop = FALSE])
if (!is_null(weight)) {
weight <- check_weight(weight, n)
weight <- check_weight(weight[i + 1], n)
}

i[sample.int(n, size, replace = replace, prob = weight)]
Expand Down