{"action":"opened","issue":{"url":"https://api.github.com/repos/hadley/dplyr/issues/944","labels_url":"https://api.github.com/repos/hadley/dplyr/issues/944/labels{/name}","comments_url":"https://api.github.com/repos/hadley/dplyr/issues/944/comments","events_url":"https://api.github.com/repos/hadley/dplyr/issues/944/events","html_url":"https://github.com/hadley/dplyr/issues/944","id":56406231,"number":944,"title":"Sweep over several columns simultaneously","user":{"login":"klmr","id":38931,"avatar_url":"https://avatars.githubusercontent.com/u/38931?v=3","gravatar_id":"","url":"https://api.github.com/users/klmr","html_url":"https://github.com/klmr","followers_url":"https://api.github.com/users/klmr/followers","following_url":"https://api.github.com/users/klmr/following{/other_user}","gists_url":"https://api.github.com/users/klmr/gists{/gist_id}","starred_url":"https://api.github.com/users/klmr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/klmr/subscriptions","organizations_url":"https://api.github.com/users/klmr/orgs","repos_url":"https://api.github.com/users/klmr/repos","events_url":"https://api.github.com/users/klmr/events{/privacy}","received_events_url":"https://api.github.com/users/klmr/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-02-03T17:05:00Z","updated_at":"2015-02-03T17:05:00Z","closed_at":null,"body":"`sweep` allows sweeping a summary over an array. Example from the help file:\r\n\r\n```r\r\nrequire(stats)\r\nmed.att <- apply(attitude, 2, median)\r\nsweep(data.matrix(attitude), 2, med.att) # subtract the column medians\r\n```\r\n\r\nIn specific cases such as this, there are ways of solving elegantly this in dplyr. However, [the general case, e.g. `scale`, is more complicated](http://stackoverflow.com/q/28298688/1968).\r\n\r\nAt the moment, the most direct dplyr equivalent of the general `sweep(x, 2, stat, fun)` seems to be:\r\n\r\n```r\r\nstat = as.data.frame(t(as.data.frame(stat)))\r\nmutate_each(x, funs(fun(., stat$.))\r\n```\r\n\r\nWhich is far from intuitive, especially concerning the access to the summary statistic."}}