{"action":"opened","issue":{"url":"https://api.github.com/repos/hadley/tidyr/issues/101","labels_url":"https://api.github.com/repos/hadley/tidyr/issues/101/labels{/name}","comments_url":"https://api.github.com/repos/hadley/tidyr/issues/101/comments","events_url":"https://api.github.com/repos/hadley/tidyr/issues/101/events","html_url":"https://github.com/hadley/tidyr/issues/101","id":104496752,"number":101,"title":"replace_na() should respect grouping.","user":{"login":"npjc","id":6063192,"avatar_url":"https://avatars.githubusercontent.com/u/6063192?v=3","gravatar_id":"","url":"https://api.github.com/users/npjc","html_url":"https://github.com/npjc","followers_url":"https://api.github.com/users/npjc/followers","following_url":"https://api.github.com/users/npjc/following{/other_user}","gists_url":"https://api.github.com/users/npjc/gists{/gist_id}","starred_url":"https://api.github.com/users/npjc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/npjc/subscriptions","organizations_url":"https://api.github.com/users/npjc/orgs","repos_url":"https://api.github.com/users/npjc/repos","events_url":"https://api.github.com/users/npjc/events{/privacy}","received_events_url":"https://api.github.com/users/npjc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-09-02T14:44:21Z","updated_at":"2015-09-02T14:44:21Z","closed_at":null,"body":"#### `replace_na()` should respect grouping:\r\n\r\n``` r\r\nlibrary(dplyr)\r\nlibrary(tidyr)\r\ndf <- data_frame(x = c(\"a\", \"b\", \"b\"), idx = c(1, 1:2), y = c(\"only-obs\", \"obs-1\",\"obs-2\"))\r\ndf\r\n#> Source: local data frame [3 x 3]\r\n#> \r\n#>       x   idx        y\r\n#>   (chr) (dbl)    (chr)\r\n#> 1     a     1 only-obs\r\n#> 2     b     1    obs-1\r\n#> 3     b     2    obs-2\r\n```\r\n\r\nnotice that for `x == \"a\"` there is only one y observation: `\"only-obs\"`.\r\n\r\n``` r\r\ndf %>% complete(x, idx)\r\n#> Source: local data frame [4 x 3]\r\n#> \r\n#>       x   idx        y\r\n#>   (chr) (dbl)    (chr)\r\n#> 1     a     1 only-obs\r\n#> 2     a     2       NA\r\n#> 3     b     1    obs-1\r\n#> 4     b     2    obs-2\r\n```\r\n\r\nI wish that `replace_na()` would respect grouping so that I could compute the value to replace with based on the current group. In this case you get the desired outcome because it is taking the first one (hence warning).\r\n\r\n``` r\r\ndf %>% \r\n  complete(x, idx) %>% \r\n  group_by(x) %>% \r\n  replace_na(list(y = .$y[!is.na(.$y)]))\r\n#> Warning in data[[var]][is.na(data[[var]])] <- replace[[var]]: number of\r\n#> items to replace is not a multiple of replacement length\r\n#> Source: local data frame [4 x 3]\r\n#> \r\n#>       x   idx        y\r\n#>   (chr) (dbl)    (chr)\r\n#> 1     a     1 only-obs\r\n#> 2     a     2 only-obs\r\n#> 3     b     1    obs-1\r\n#> 4     b     2    obs-2\r\n```"}}