This might be a trivial thing but I'm seeing a peculiar case and i thought to validate with the community.
I have a data frame with the following dimensions $pid : num $group : chr $status : chr...
df <- tibble::tribble(
~pid, ~group,~status,
12, "g1", 1,
12, "g2", 0,
18, "g3", 1,
18, "g1", 1,
18, "g2", 1
)
Now while working on window functions I need to apply cumsum() over each group of 'pid' so Im using the following code
r2 <- df%>%
group_by(pid)%>%
mutate(col = cumsum(status))
And I'm expecting r2 to be
pid group status col
12 g1 1 1
12 g2 0 1
18 g3 1 1
18 g1 1 2
18 g2 1 3
But my resultant r2 is not so. On the contrary
pid group status col
12 g1 1 1
12 g2 0 1
18 g3 1 2
18 g1 1 3
18 g2 1 4
Which to me looked like it is not creating a 'window' over the pid column. I tried converting the pid to character but still the same result.
If my understanding of cumsum is correct, what could the possible reason be for such behaviour.
As per packages are concerned, I've dplyr, plyr, sqldf, data.table, lubridate loaded in my workspace