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

'nth' inside 'summarize' returns an error when 'which' is used to calculate the argument 'n' #734

Closed
CameronBieganek opened this issue Oct 29, 2014 · 3 comments
Assignees
Labels
Milestone

Comments

@CameronBieganek
Copy link

@CameronBieganek CameronBieganek commented Oct 29, 2014

Here's a toy example:

> set.seed(11)
> df <- data.frame(x = c(1:4, 7:9, 13:19), y = sample(100:999, 14))
> df
    x   y
1   1 349
2   2 100
3   3 558
4   4 112
5   7 157
6   8 954
7   9 177
8  13 358
9  14 885
10 15 209
11 16 255
12 17 491
13 18 905
14 19 854
> which(df$x == 16)
[1] 11
> summarize(
+     df,
+     abc = nth(y, n = which(x == 16))
+ )
Error: 'n' should be a scalar integer
@hadley
Copy link
Member

@hadley hadley commented Oct 29, 2014

Why are you doing that and not just y[which(x == 16)]?

@CameronBieganek
Copy link
Author

@CameronBieganek CameronBieganek commented Oct 29, 2014

Because I want to use this feature of nth:

> vec <- 5:8
> vec[0]
integer(0)
> nth(vec, n = 0)
[1] NA

My data frame has some missing data, so sometimes the boolean inside the which is all false, and then which returns integer(0). So my actual use case looks something like this:

summarize(
    df,
    abc = nth(y, n = sum(which(x == 16)))
)

That way when the which fails to find anything and returns integer(0), the sum will return 0, and the nth should return NA.

@hadley hadley added the feature label Oct 30, 2014
@hadley hadley added this to the 0.4 milestone Oct 30, 2014
@romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Nov 11, 2014

What I've done is fallback to R evaluation whenever n is not a length one integer or numeric, e.g. an expression for R to evaluate.

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

Successfully merging a pull request may close this issue.

None yet
3 participants