-1

I have this simplified df that I'm tryng to get the cumsum() for each group

library(dplyr)

Country <- c(rep("A",5), rep("B",4), rep("C", 5))
Date <- 1:14
Value <- c(2:6, 10:13, 7:11)

df <- data.frame(Date, Country, Value)
> view(df)

    Date Country Value
     1       A     2
     2       A     3
     3       A     4
     4       A     5
     5       A     6
     6       B    10
     7       B    11
     8       B    12
     9       B    13
    10       C     7
    11       C     8
    12       C     9
    13       C    10
    14       C    11

I know it seems like a repeated question, but I'm doing the following and don't know why isn't working. Instead of the cumulative sum by group, I'm getting the cumsum of all column:

df_sum <- df%>%
    group_by(Country) %>%
  mutate(Sum = cumsum(Value))


   Date Country Value   Sum
   <int> <chr>   <int> <int>
      1  A           2     2
      2  A           3     5
      3  A           4     9
      4  A           5    14
      5  A           6    20
      6  B          10    30
      7  B          11    41
      8  B          12    53
      9  B          13    66
      10 C           7    73
      11 C           8    81
      12 C           9    90
      13 C          10   100
      14 C          11   111

Jones
  • 333
  • 1
  • 11
  • 1
    If I copy/paste your code, I do not get the same result. Have you tried this in a new R session? Are you sure it's reproducible on your end? – MrFlick Dec 15 '20 at 19:14
  • It's reproducible. I think the problem is a package that I installed yesterday and somehow messed with other packages. Thank you anyway – Jones Dec 15 '20 at 19:19
  • Well, what packages do you have loaded? What does your `sessionInfo()` look like? In a new R session nothing should be loaded automatically so maybe you are running more code than you are showing here. – MrFlick Dec 15 '20 at 19:20
  • I loaded only dplyr lol. But the package I think messed with things was qpcR, because I had some trouble yesterday. – Jones Dec 15 '20 at 19:24
  • 1
    Can you verify that you only have `dplyr` loaded by showing us the `sessionInfo()`. Just having other packages installed shouldn't make any difference at all. It only matters if the packages are loaded. – MrFlick Dec 15 '20 at 19:25

1 Answers1

1

it is possible that the OP loaded plyr package as well which masked the dplyr::mutate with plyr::mutate. Either, do this on a fresh R session with only dplyr loaded or specify the packagename::functionname dplyr::mutate

library(dplyr)
df%>%
  group_by(Country) %>%
  dplyr::mutate(Sum = cumsum(Value)) %>%
  ungroup

-output

# A tibble: 14 x 4
#    Date Country Value   Sum
#   <int> <chr>   <int> <int>
# 1     1 A           2     2
# 2     2 A           3     5
# 3     3 A           4     9
# 4     4 A           5    14
# 5     5 A           6    20
# 6     6 B          10    10
# 7     7 B          11    21
# 8     8 B          12    33
# 9     9 B          13    46
#10    10 C           7     7
#11    11 C           8    15
#12    12 C           9    24
#13    13 C          10    34
#14    14 C          11    45
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    I think the problem is a package (qpcR) that I installed yesterday and somehow messed with other packages. The code you made worked fine. Thank you! – Jones Dec 15 '20 at 19:21