Hi Folks, I am trying to get a group_by cumsum using: R version 3.5.0 (2018-04-23) -- "Joy in Playing" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin15.6.0 (64-bit) Here is an example of a simple construct that is not working. m <- data.frame( id = rep(1:3, each=2), score = rep(c(6,3), each=3) ) m %>% group_by(id) %>% mutate(total = cumsum(score)) My output: # A tibble: 6 x 3 # Groups: id [3] id score total <int> <dbl> <dbl> 1 1 6 6 2 1 6 12 3 2 6 18 4 2 3 21 5 3 3 24 6 3 3 27 What am I missing? Thanks in advance. Regards, Sumit
Strange. Worked fine on: R version 3.4.2 (2017-09-28) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Debian GNU/Linux 9 (stretch) Commands: m <- data.frame( id = rep(1:3, each=2), score = rep(c(6,3), each=3) ) as.data.frame(m %>% group_by(id) %>% mutate(total = cumsum(score))) id score total 1 1 6 6 2 1 6 12 3 2 6 6 4 2 3 9 5 3 3 3 6 3 3 6 -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Sumitrajit Dhar Sent: Monday, July 9, 2018 5:35 PM To: r-help at r-project.org Subject: [R] Something simple not working in group_by Hi Folks, I am trying to get a group_by cumsum using: R version 3.5.0 (2018-04-23) -- "Joy in Playing" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin15.6.0 (64-bit) Here is an example of a simple construct that is not working. m <- data.frame( id = rep(1:3, each=2), score = rep(c(6,3), each=3) ) m %>% group_by(id) %>% mutate(total = cumsum(score)) My output: # A tibble: 6 x 3 # Groups: id [3] id score total <int> <dbl> <dbl> 1 1 6 6 2 1 6 12 3 2 6 18 4 2 3 21 5 3 3 24 6 3 3 27 What am I missing? Thanks in advance. Regards, Sumit
Dunno. But if I understand correctly, here's a base R way to do it: (## using your m)> m$total <- with(m,ave(score,id,FUN = cumsum)) > mid score total 1 1 6 6 2 1 6 12 3 2 6 6 4 2 3 9 5 3 3 3 6 3 3 6 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Jul 9, 2018 at 2:35 PM, Sumitrajit Dhar <s-dhar at northwestern.edu> wrote:> Hi Folks, > > I am trying to get a group_by cumsum using: > > R version 3.5.0 (2018-04-23) -- "Joy in Playing" > Copyright (C) 2018 The R Foundation for Statistical Computing > Platform: x86_64-apple-darwin15.6.0 (64-bit) > > Here is an example of a simple construct that is not working. > > m <- data.frame( id = rep(1:3, each=2), score = rep(c(6,3), each=3) ) > > m %>% group_by(id) %>% mutate(total = cumsum(score)) > > My output: > > # A tibble: 6 x 3 > # Groups: id [3] > id score total > <int> <dbl> <dbl> > 1 1 6 6 > 2 1 6 12 > 3 2 6 18 > 4 2 3 21 > 5 3 3 24 > 6 3 3 27 > > What am I missing? Thanks in advance. > > Regards, > Sumit > > > ______________________________________________ > R-help at 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]]
Hi Sumit, I was not able to reproduce this problem. I tried it in both R 3.5.1 and R 3.4.4. Both gave the expected output (which differs from yours.) Eric On Tue, Jul 10, 2018 at 1:32 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:> Dunno. > > But if I understand correctly, here's a base R way to do it: > > (## using your m) > > > m$total <- with(m,ave(score,id,FUN = cumsum)) > > m > id score total > 1 1 6 6 > 2 1 6 12 > 3 2 6 6 > 4 2 3 9 > 5 3 3 3 > 6 3 3 6 > > Cheers, > Bert > > > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along and > sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > On Mon, Jul 9, 2018 at 2:35 PM, Sumitrajit Dhar <s-dhar at northwestern.edu> > wrote: > > > Hi Folks, > > > > I am trying to get a group_by cumsum using: > > > > R version 3.5.0 (2018-04-23) -- "Joy in Playing" > > Copyright (C) 2018 The R Foundation for Statistical Computing > > Platform: x86_64-apple-darwin15.6.0 (64-bit) > > > > Here is an example of a simple construct that is not working. > > > > m <- data.frame( id = rep(1:3, each=2), score = rep(c(6,3), each=3) ) > > > > m %>% group_by(id) %>% mutate(total = cumsum(score)) > > > > My output: > > > > # A tibble: 6 x 3 > > # Groups: id [3] > > id score total > > <int> <dbl> <dbl> > > 1 1 6 6 > > 2 1 6 12 > > 3 2 6 18 > > 4 2 3 21 > > 5 3 3 24 > > 6 3 3 27 > > > > What am I missing? Thanks in advance. > > > > Regards, > > Sumit > > > > > > ______________________________________________ > > R-help at 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]] > > ______________________________________________ > R-help at 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]]
Hello everyone, I figured it out late last night. I was loading both dplyr and plyr and that was causing the problem. Loading plyr after dplyr leads to the faulty behavior. Just loading dplyr gives you expected behavior. Sorry for the confusion and false alarm. Regards, Sumit> On Jul 10, 2018, at 2:48 AM, Eric Berger <ericjberger at gmail.com> wrote: > > Hi Sumit, > I was not able to reproduce this problem. > I tried it in both R 3.5.1 and R 3.4.4. > Both gave the expected output (which differs from yours.) > > Eric > > > On Tue, Jul 10, 2018 at 1:32 AM, Bert Gunter <bgunter.4567 at gmail.com <mailto:bgunter.4567 at gmail.com>> wrote: > Dunno. > > But if I understand correctly, here's a base R way to do it: > > (## using your m) > > > m$total <- with(m,ave(score,id,FUN = cumsum)) > > m > id score total > 1 1 6 6 > 2 1 6 12 > 3 2 6 6 > 4 2 3 9 > 5 3 3 3 > 6 3 3 6 > > Cheers, > Bert > > > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along and > sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > On Mon, Jul 9, 2018 at 2:35 PM, Sumitrajit Dhar <s-dhar at northwestern.edu <mailto:s-dhar at northwestern.edu>> > wrote: > > > Hi Folks, > > > > I am trying to get a group_by cumsum using: > > > > R version 3.5.0 (2018-04-23) -- "Joy in Playing" > > Copyright (C) 2018 The R Foundation for Statistical Computing > > Platform: x86_64-apple-darwin15.6.0 (64-bit) > > > > Here is an example of a simple construct that is not working. > > > > m <- data.frame( id = rep(1:3, each=2), score = rep(c(6,3), each=3) ) > > > > m %>% group_by(id) %>% mutate(total = cumsum(score)) > > > > My output: > > > > # A tibble: 6 x 3 > > # Groups: id [3] > > id score total > > <int> <dbl> <dbl> > > 1 1 6 6 > > 2 1 6 12 > > 3 2 6 18 > > 4 2 3 21 > > 5 3 3 24 > > 6 3 3 27 > > > > What am I missing? Thanks in advance. > > > > Regards, > > Sumit > > > > > > ______________________________________________ > > R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwMFaQ&c=yHlS04HhBraes5BQ9ueu5zKhE7rtNXt_d012z2PA6ws&r=hooQlNrL6cH-O78mNmvUpBZ95zI1C-jF0GfoKkuJSaU&m=CvhIldTo5hoJJi5Bd7_sHFWRdH7Fd4Jv3ExQB0RPlIU&s=r1iXOeVerM13vY8SJ43KMMSjoveKXYTCmHk2uqE9jwI&e=> > > PLEASE do read the posting guide http://www.R-project.org/ <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_&d=DwMFaQ&c=yHlS04HhBraes5BQ9ueu5zKhE7rtNXt_d012z2PA6ws&r=hooQlNrL6cH-O78mNmvUpBZ95zI1C-jF0GfoKkuJSaU&m=CvhIldTo5hoJJi5Bd7_sHFWRdH7Fd4Jv3ExQB0RPlIU&s=Qs4zmrgW7sXjofqwf0VQmV1M7Slw3pS6Mv_TpLYipqQ&e=> > > posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwMFaQ&c=yHlS04HhBraes5BQ9ueu5zKhE7rtNXt_d012z2PA6ws&r=hooQlNrL6cH-O78mNmvUpBZ95zI1C-jF0GfoKkuJSaU&m=CvhIldTo5hoJJi5Bd7_sHFWRdH7Fd4Jv3ExQB0RPlIU&s=r1iXOeVerM13vY8SJ43KMMSjoveKXYTCmHk2uqE9jwI&e=> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwMFaQ&c=yHlS04HhBraes5BQ9ueu5zKhE7rtNXt_d012z2PA6ws&r=hooQlNrL6cH-O78mNmvUpBZ95zI1C-jF0GfoKkuJSaU&m=CvhIldTo5hoJJi5Bd7_sHFWRdH7Fd4Jv3ExQB0RPlIU&s=ZJbUb--ZGIsJv2dCTON31rOiQ-MQEv3DU1ZrWM_scss&e=> > and provide commented, minimal, self-contained, reproducible code. >