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

group_by() %>% mutate(factor()) strange behavior #1414

Closed
paulanka opened this issue Sep 17, 2015 · 3 comments
Closed

group_by() %>% mutate(factor()) strange behavior #1414

paulanka opened this issue Sep 17, 2015 · 3 comments
Assignees

Comments

@paulanka
Copy link

@paulanka paulanka commented Sep 17, 2015

Using factor() in mutate on a grouped data frame gives strange results

require(Lahman)
d <- Batting %>% group_by(lgID,yearID) %>%
   summarise(s = sum(G)) %>%
   mutate(f0=s>9000,
               f1=factor(s>9000))
xtabs(~f0,d) # 20 250
xtabs(~f1,d) # 136 134 ( wrong)

using ungroup() before mutate() solves the problem

d <- Batting %>% group_by(lgID,yearID) %>%
  summarise(s = sum(G)) %>%
  ungroup() %>%
  mutate(f0=s>9000,
   f1=factor(s>9000))
xtabs(~f0,d) # 20 250
xtabs(~f1,d) # 20 250
@romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Sep 18, 2015

This is because the distribution of unique values is not the same across groups:

> fact <- function(x) { print(unique(x)); factor(x) }
>
>
> d <- Batting %>% group_by(lgID,yearID) %>%
+    summarise(s = sum(G)) %>%
+    mutate(f0=s>9000,f1= fact(s>9000) )
[1] FALSE  TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE  TRUE
[1] TRUE
[1] FALSE

@romainfrancois romainfrancois self-assigned this Sep 18, 2015
@paulanka
Copy link
Author

@paulanka paulanka commented Sep 18, 2015

Thank you for your quick reply. This is a bit tricky. Maybe the doc should warn about using factor with grouped df.

@romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Sep 18, 2015

Nah that's a bug. I'll probably pick it up tonight.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 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 pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants