[R] dplyr instead of plyr: Help
Thierry Onkelinx
thierry.onkelinx at inbo.be
Mon Nov 2 13:29:23 CET 2015
Please don't post in HTML. It makes your code unreadable.
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
2015-11-02 13:13 GMT+01:00 Axel Urbiz <axel.urbiz op gmail.com>:
> Sorry, this is *related* to a recent post, but not the same. I'd appreciate
> your help in getting the same results with the two methods below (the first
> using plyr and the second using dplyr. The former works, but not the
> latter.)
>
>
> ### Sample data
> set.seed(4)df <- data.frame(pred = rnorm(100), y = sample(c(0,1), 100,
> replace = TRUE), models = gl(2, 50, 100, labels =
> c("model1", "model2")))
> ### using plyr
> fooFun <- function(x) { xcuts <- unique(x$pred) x$bin <- cut(x$pred,
> breaks = xcuts, include.lowest = TRUE) x <-
> dplyr::summarize(dplyr::group_by(x, bin), sumY = sum(y))
> x}head(plyr::ddply(df, plyr::.(models), fooFun))
>
> ### Using dplyr
> fooFun2 <- function(pred, y) { xcuts <- unique(pred) bin <- cut(pred,
> breaks = xcuts, include.lowest = TRUE) dft <- data.frame(bin, pred, y)
> dft <- dplyr::summarize(dplyr::group_by(dft, bin), sumY = sum(y)) dft}
> res_dplyr <- dplyr::mutate(dplyr::group_by(df, models), fooFun2(pred,
> y))Error: incompatible size (2), expecting 50 (the group size) or 1
> head(res_dplyr)
> Thank you.
> Axel.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list