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

Using ifelse with NAs in mutate sometimes results in strange coercion behavior #299

Closed
joranE opened this issue Mar 4, 2014 · 5 comments
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@joranE
Copy link
Contributor

joranE commented Mar 4, 2014

In 0.1.2,

d <- data.frame(x = rep(1:5, each = 3))
mutate(group_by(d,x),val = ifelse(x < 3, NA, 2)) #Coerces to logical
mutate(group_by(d,x),val = ifelse(x < 3, NA_real_, 2)) #Works

It seems to be very subtle, because this does not have the same behavior:

d1 <- data.frame(x = rep(1:3,each = 3),y = rep(1:3,times = 3))
mutate(group_by(d1,x),val = ifelse(y < 3, NA, 5))
@hadley
Copy link
Member

hadley commented Mar 5, 2014

@romainfrancois do we have a custom version of ifelse in the hybrid evaluator?

@romainfrancois
Copy link
Member

Not yet. I'll look into it. Might be hard.

Envoyé de mon iPhone

Le 5 mars 2014 à 16:47, Hadley Wickham notifications@github.com a écrit :

@romainfrancois do we have a custom version of ifelse in the hybrid evaluator?


Reply to this email directly or view it on GitHub.

@hadley hadley added the bug label Mar 17, 2014
@hadley hadley added this to the v0.2 milestone Mar 17, 2014
@romainfrancois
Copy link
Member

What happens here is that the first group has the special responsability of finding the eventual type of the result column. Maybe I can check that subsequent groups produce compatible results and at least throw an error in that case as a first measure.

@romainfrancois
Copy link
Member

I've put this in place:

> mutate(group_by(d,x),val = ifelse(x < 3, NA, 2)) 
Error : incompatible types, expecting a logical vector

Maybe we can instead do promotions, etc ... but that's more work

@hadley
Copy link
Member

hadley commented Mar 20, 2014

That seems reasonable. At some point we'll need some general coercion mechanism that can work for this, rbind, joins, etc.

@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
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants