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

NULL in Mutate as result of expression #2945

Closed
aaronjg opened this issue Jul 5, 2017 · 11 comments
Closed

NULL in Mutate as result of expression #2945

aaronjg opened this issue Jul 5, 2017 · 11 comments
Labels

Comments

@aaronjg
Copy link

@aaronjg aaronjg commented Jul 5, 2017

If a column is set to NULL in mutate it is deleted. However, this does not work if mutate is returned as a result of an expression. It would also be nice if you could use NULL in summarize to exclude a column as well, but maybe that should go into a new feature request?

> df <- data.frame(a=5,b=6) 
> df %>% mutate(a= NULL)
  b
1 6
> z <- NULL
> df %>% mutate(a=z)
Error: Unsupported type NILSXP for column "a

Previous discussion of the NULL feature.
#462
8104dd6

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Jul 12, 2017

Thanks. This is by design, in particular because for a grouped mutate() it would be unclear what to do if the mutator returns NULL for some groups only. For summarize() we don't seem to need this, because the result includes only the grouping variables and those that are explicitly created.

@hadley
Copy link
Member

@hadley hadley commented Jul 13, 2017

Don't we have another open issue about this?

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Jul 13, 2017

We decided to improve error messages only, but apparently we did this only for the ungrouped case... #2187

@krlmlr krlmlr added this to the 0.7.3 milestone Aug 16, 2017
@krlmlr krlmlr added this to the 0.7.3 milestone Aug 16, 2017
@krlmlr
Copy link
Member

@krlmlr krlmlr commented Aug 22, 2017

I'm now getting:

library(dplyr)
z <- NULL
data.frame(a=5,b=6) %>% group_by(b) %>% mutate(a=z)
#> Error in mutate_impl(.data, dots): Column `a` is of unsupported type NULL

Same for ungrouped data.

@hadley: Do we need to add a special-case for the wording of the error message here?

@hadley
Copy link
Member

@hadley hadley commented Aug 22, 2017

The error message is ok for now. I'd rather make it possible to remove the column by setting it to NULL.

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Aug 22, 2017

We don't remove because it's unclear what to do in a grouped call when it's removed only for some groups:

tibble(a = 1:3, b = 4:6) %>%
  group_by(a) %>%
  mutate(b = if (a < 2) NULL else b)

@hadley
Copy link
Member

@hadley hadley commented Aug 22, 2017

Right, but that's mostly an artificial example, and could presumably be handled in the same way as

tibble(a = 1:3, b = 4:6) %>%
  group_by(a) %>%
  mutate(b = if (a < 2) 1 else "b")

@aaronjg
Copy link
Author

@aaronjg aaronjg commented Aug 22, 2017

But similar issues can happen with differing group lengths, and in that case, an error is thrown only when the lengths are invalid:

> tibble(a = 1:3, b = 4:6) %>% group_by(a) %>% mutate(b = if (a < 2) rep(1,b) else b)
Error in mutate_impl(.data, dots) : 
   Column `b` must be length 1 (the group size), not 4

@krlmlr krlmlr added feature and removed bug labels Aug 22, 2017
@krlmlr krlmlr removed this from the 0.7.3 milestone Aug 22, 2017
@krlmlr
Copy link
Member

@krlmlr krlmlr commented Aug 22, 2017

We should postpone this until after #2311.

@hadley
Copy link
Member

@hadley hadley commented Aug 22, 2017

Agreed.

@lock
Copy link

@lock lock bot commented Nov 26, 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 26, 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