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

sample_n() and sample_frac() can use n() #3527

Merged
merged 3 commits into from May 2, 2018
Merged

Conversation

@romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Apr 23, 2018

the two functions are now based on slice, which allows to tap into hybrid such as n(), n_distinct() etc ...

as a bonus, we no longer need the grouped_df version because slice already takes care of evaluating the expression within groups.

#' @export
sample_n.data.frame <- function(tbl, size, replace = FALSE,
                                weight = NULL, .env = NULL) {
  if (!is_null(.env)) {
    inform("`.env` is deprecated and no longer has any effect")
  }

  size <- enquo(size)
  weight <- enquo(weight)

  weight <- enquo(weight)
  slice(tbl, sample.int(n(), check_size(!!size, n(), replace = replace), replace = replace, prob = !!weight))
}


#' @export
sample_frac.data.frame <- function(tbl, size = 1, replace = FALSE,
                                   weight = NULL, .env = NULL) {
  if (!is_null(.env)) {
    inform("`.env` is deprecated and no longer has any effect")
  }

  size <- enquo(size)
  weight <- enquo(weight)

  slice(tbl, sample.int(n(), round(n() * check_frac(!!size, replace = replace)), replace = replace, prob = !!weight))
}
Copy link
Member

@krlmlr krlmlr left a comment

Lovely!

R/dataframe.R Outdated
size <- enquo(size)
weight <- enquo(weight)

weight <- enquo(weight)
Copy link
Member

@krlmlr krlmlr Apr 23, 2018

This looks redundant.

Copy link
Member Author

@romainfrancois romainfrancois Apr 23, 2018

it is. I probably forgot to push my last commit 😬

Copy link
Member

@krlmlr krlmlr May 2, 2018

?


idx <- sample.int(n, size, replace = replace, prob = weight)
tbl[idx, , drop = FALSE]
slice(tbl, sample.int(n(), round(n() * check_frac(!!size, replace = replace)), replace = replace, prob = !!weight))
Copy link
Member

@krlmlr krlmlr Apr 23, 2018

Is round() the correct operation here?

Copy link
Member Author

@romainfrancois romainfrancois Apr 23, 2018

this is what the original code did

krlmlr
krlmlr approved these changes May 2, 2018
Copy link
Member

@krlmlr krlmlr left a comment

Can you please merge?

R/dataframe.R Outdated
size <- enquo(size)
weight <- enquo(weight)

weight <- enquo(weight)
Copy link
Member

@krlmlr krlmlr May 2, 2018

?

@romainfrancois romainfrancois merged commit 14d5dd9 into master May 2, 2018
0 of 4 checks passed
@romainfrancois romainfrancois deleted the feature-3413-sample_n branch May 8, 2018
@lock
Copy link

@lock lock bot commented Nov 4, 2018

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 Nov 4, 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 issues

Successfully merging this pull request may close these issues.

None yet

2 participants