{"action":"created","issue":{"url":"https://api.github.com/repos/hadley/dplyr/issues/1545","repository_url":"https://api.github.com/repos/hadley/dplyr","labels_url":"https://api.github.com/repos/hadley/dplyr/issues/1545/labels{/name}","comments_url":"https://api.github.com/repos/hadley/dplyr/issues/1545/comments","events_url":"https://api.github.com/repos/hadley/dplyr/issues/1545/events","html_url":"https://github.com/hadley/dplyr/issues/1545","id":117965899,"number":1545,"title":"Group_By + Mutate with NA misses groups","user":{"login":"acjackman","id":1128992,"avatar_url":"https://avatars.githubusercontent.com/u/1128992?v=3","gravatar_id":"","url":"https://api.github.com/users/acjackman","html_url":"https://github.com/acjackman","followers_url":"https://api.github.com/users/acjackman/followers","following_url":"https://api.github.com/users/acjackman/following{/other_user}","gists_url":"https://api.github.com/users/acjackman/gists{/gist_id}","starred_url":"https://api.github.com/users/acjackman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acjackman/subscriptions","organizations_url":"https://api.github.com/users/acjackman/orgs","repos_url":"https://api.github.com/users/acjackman/repos","events_url":"https://api.github.com/users/acjackman/events{/privacy}","received_events_url":"https://api.github.com/users/acjackman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-20T05:01:05Z","updated_at":"2016-02-16T02:46:22Z","closed_at":null,"body":"I seem to be having a similar issue. I think that it seems to be the NA values that cause the problem. I've also had a problem with summarize in addition to mutate. They seem to run the functions twice for some groups and miss others completely.\r\n\r\nA sample from a real life data set is included (\r\n[na_trbl.txt](https://github.com/hadley/dplyr/files/39758/na_trbl.txt)\r\n):\r\n\r\n    dta <- read.csv(\"na_trbl.csv\")\r\n    dta\r\n       cid nc\r\n    1    1 NA\r\n    2    1 NA\r\n    3    1 NA\r\n    4    2 NA\r\n    5    2 NA\r\n    6    3 24\r\n    7    3 24\r\n    8    3 NA\r\n    9    3 NA\r\n    10   4 15\r\n    11   4 26\r\n    12   4 15\r\n    13   5 28\r\n    14   5 22\r\n    15   5 40\r\n    16   6 NA\r\n\r\n\r\nI created a function that prints each time it is run to see how the group_by was affecting mutate.\r\n\r\n    trbl <- function(value,print){\r\n        cat(print, \" - \", value, \"\\n\")\r\n        value\r\n    }\r\n\r\nRunning the following code provides some interesting results.\r\n\r\n    trbl_mutate <-\r\n        dta %>%\r\n        filter(cid != 1) %>%\r\n        dplyr::group_by(cid) %>%\r\n        dplyr::mutate(\r\n            mutatevar = trbl(nc, cid)\r\n        )\r\n\r\nThe `trbl` function prints this to the console:\r\n\r\n    1 1 1  -  NA NA NA \r\n    1 1 1  -  NA NA NA \r\n    2 2  -  NA NA \r\n    3 3 3 3  -  24 24 NA NA \r\n    5 5 5  -  28 22 40 \r\n    6  -  NA \r\n\r\nNotice that the group `1` is duplicated, while `4` isn't printed at all.\r\n\r\nLooking at the results, we see that the variable `mutatevar`  for group `4` has `NA` values where it should have just copped the values across.\r\n\r\n    R> trbl_mutate\r\n    Source: local data frame [16 x 3]\r\n    Groups: cid [6]\r\n         cid    nc mutatevar\r\n       (int) (int)     (int)\r\n    1      1    NA        NA\r\n    2      1    NA        NA\r\n    3      1    NA        NA\r\n    4      2    NA        NA\r\n    5      2    NA        NA\r\n    6      3    24        24\r\n    7      3    24        24\r\n    8      3    NA        NA\r\n    9      3    NA        NA\r\n    10     4    15        NA\r\n    11     4    26        NA\r\n    12     4    15        NA\r\n    13     5    28        28\r\n    14     5    22        22\r\n    15     5    40        40\r\n    16     6    NA        NA\r\n\r\nIt might be helpful to know that the summarise function also has the group duplication issue. A reproducible example:\r\n\r\n    trbl_summarise <- \r\n        dta %>%\r\n        dplyr::group_by(cid) %>%\r\n        dplyr::summarise(\r\n            summarisevar = mean(trbl(nc, cid), na.rm=TRUE)\r\n        )\r\n\r\nThe `trbl` function prints:\r\n\r\n    1 1 1  -  NA NA NA \r\n    2 2  -  NA NA \r\n    3 3 3 3  -  24 24 NA NA \r\n    3 3 3 3  -  24 24 NA NA \r\n    4 4 4  -  15 26 15 \r\n    5 5 5  -  28 22 40 \r\n    6  -  NA \r\n\r\nWhile the output of the computation\r\n\r\n    R> trbl_summarise\r\n    Source: local data frame [6 x 2]\r\n        cid sumarisevar\r\n      (int)       (dbl)\r\n    1     1          NA\r\n    2     2          NA\r\n    3     3    24.00000\r\n    4     4    18.66667\r\n    5     5    30.00000\r\n    6     6         NaN\r\n\r\nAny help or explanation if this functionality is expected would be greatly appreciated!"},"comment":{"url":"https://api.github.com/repos/hadley/dplyr/issues/comments/184486647","html_url":"https://github.com/hadley/dplyr/issues/1545#issuecomment-184486647","issue_url":"https://api.github.com/repos/hadley/dplyr/issues/1545","id":184486647,"user":{"login":"dgrtwo","id":994718,"avatar_url":"https://avatars.githubusercontent.com/u/994718?v=3","gravatar_id":"","url":"https://api.github.com/users/dgrtwo","html_url":"https://github.com/dgrtwo","followers_url":"https://api.github.com/users/dgrtwo/followers","following_url":"https://api.github.com/users/dgrtwo/following{/other_user}","gists_url":"https://api.github.com/users/dgrtwo/gists{/gist_id}","starred_url":"https://api.github.com/users/dgrtwo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgrtwo/subscriptions","organizations_url":"https://api.github.com/users/dgrtwo/orgs","repos_url":"https://api.github.com/users/dgrtwo/repos","events_url":"https://api.github.com/users/dgrtwo/events{/privacy}","received_events_url":"https://api.github.com/users/dgrtwo/received_events","type":"User","site_admin":false},"created_at":"2016-02-16T02:46:22Z","updated_at":"2016-02-16T02:46:22Z","body":"I believe this behavior is now fixed on the dev version. I suspect it's from the addition of [this line](https://github.com/hadley/dplyr/commit/8691bce9370d81900c95148f5f78fce763225d2e#diff-3e9f2fe3fe00f9343478a1d105eadf0bR32) in 8691bce9370d81900c95148f5f78fce763225d2e."}}