Lucia
Lucia

Reputation: 647

Sum up values according to time R

I need to sum up the values according to the timeline, here's the data

           userid user_count       time
215981 1702099122          1 2014-10-16
762721 2631243080          1 2014-11-17
806291 2753297247          1 2014-10-13
927621 3177288950          1 2014-11-22
136961 1632673193          1 2015-10-12
374601 1801088453          1  2015-11-9

If I use aggregate to add a column called user_time

user_time <- aggregate(user_count ~time, df, sum)

Then I will get the total user_count on each day, user_time will be all 1. However, I want to compute the sum up to each day. For example, user_time on 2014-11-22 should be 4, on 2014-10-16 should be 2. I wonder how to do it properly. Thank you.

Upvotes: 1

Views: 84

Answers (1)

akrun
akrun

Reputation: 887241

Perhaps we need a cumsum

library(dplyr)
df %>% 
     arrange(time) %>%
     mutate(Count = cumsum(user_count))

Or using base R

 transform(df[order(df$time),], Count = cumsum(user_count))

Upvotes: 2

Related Questions