{"action":"opened","issue":{"url":"https://api.github.com/repos/hadley/dplyr/issues/968","labels_url":"https://api.github.com/repos/hadley/dplyr/issues/968/labels{/name}","comments_url":"https://api.github.com/repos/hadley/dplyr/issues/968/comments","events_url":"https://api.github.com/repos/hadley/dplyr/issues/968/events","html_url":"https://github.com/hadley/dplyr/issues/968","id":57659586,"number":968,"title":"Vectorized operators with na.rm argument (psum(), pmult(), etc.)","user":{"login":"garrettgman","id":85014,"avatar_url":"https://avatars.githubusercontent.com/u/85014?v=3","gravatar_id":"","url":"https://api.github.com/users/garrettgman","html_url":"https://github.com/garrettgman","followers_url":"https://api.github.com/users/garrettgman/followers","following_url":"https://api.github.com/users/garrettgman/following{/other_user}","gists_url":"https://api.github.com/users/garrettgman/gists{/gist_id}","starred_url":"https://api.github.com/users/garrettgman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/garrettgman/subscriptions","organizations_url":"https://api.github.com/users/garrettgman/orgs","repos_url":"https://api.github.com/users/garrettgman/repos","events_url":"https://api.github.com/users/garrettgman/events{/privacy}","received_events_url":"https://api.github.com/users/garrettgman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-02-13T21:57:45Z","updated_at":"2015-02-13T21:57:45Z","closed_at":null,"body":"`sum` and `+` leave a gap when working with `NA`s:\r\n\r\n    a <- c(NA, 2, 3, NA)\r\n    b <- c(1, NA, 3, NA)\r\n    c <- c(1, 2, NA, NA)\r\n    nas <- data_frame(a, b, c)\r\n    nas\r\n    ##  a  b  c\r\n    ## NA  1  1\r\n    ##  2 NA  2\r\n    ##  3  3 NA\r\n    ## NA NA NA\r\n\r\n    # no na.rm option\r\n    nas %>% mutate(d = a + b + c)\r\n    ##    a  b  c  d\r\n    ## 1 NA  1  1 NA\r\n    ## 2  2 NA  2 NA\r\n    ## 3  3  3 NA NA\r\n    ## 4 NA NA NA NA\r\n\r\n    # not what we want\r\n    nas %>% mutate(d = sum(a, b, c, na.rm = TRUE))\r\n    ##    a  b  c  d\r\n    ## 1 NA  1  1 12\r\n    ## 2  2 NA  2 12\r\n    ## 3  3  3 NA 12\r\n    ## 4 NA NA NA 12\r\n\r\nSomething that works like this would be useful. I'm sure there's a better way to implement it\r\n\r\n    psum <- function(..., na.rm = TRUE) {\r\n      rowSums(as.data.frame(list(...)), na.rm = na.rm)\r\n    }\r\n    nas %>% mutate(d = psum(a, b, c))\r\n    ##    a  b  c  d\r\n    ## 1 NA  1  1  2\r\n    ## 2  2 NA  2  4\r\n    ## 3  3  3 NA  6\r\n    ## 4 NA NA NA  0\r\n\r\nOther useful functions would be `psum`, `pprod`, `pmean`, `psd`, `pall`, `pany`.\r\n\r\n    # e.g.\r\n    nas %>% filter(d = pall(is.na(a), is.na(b), is.na(c)))\r\n    ##    a  b  c\r\n    ## 4 NA NA NA\r\n"}}