To make this question more generalized, I believe it could also be rephrased as: Creating a rolling temporally sensitive factor variable. Though an uncommon requirement, this could be utilized for many different data sources.
I have a series of non-uniform time data
with > 1 record per day for thousands of users. I want to create a new column player_type
that keeps track of a rolling 30 day definition of their behavior. The behavior is defined by what games they play; the column 'games'
is a factor of gameA, gameB.
There are thus three types of behaviors:
- Exclusively plays GameA -
'A'
- Exclusively plays GameB -
'B'
- Plays both games -
'Hybrid'
I want to use this new column to see the changes in their play behavior over time, as well as counting the number of players in each group throughout time, to see how they change.
The time series is highly irregular for each player. Players can play multiple types of games per day, or not play any games for many months. The time series is irregular per player such that a record is only created when the player plays a game, thus I expect a solution might use a filter something like:
interval(current_date, current_date - new_period(days=30)
(using lubridate).
Here is an example data set. Keep in mind this it is simplified and tests a rolling 1 day change, so simple methods checking the record before will not actually work. If you are able to make a better data set, please advise and I will edit this post.
p <- c( 1, 1, 1, 2, 2, 2, 6, 6, 6)
g <- c('A', 'B', 'B', 'A', 'B', 'A', 'A', 'B', 'B')
d <- seq(as.Date('2014-10-01'), as.Date('2014-10-9'), by=1)
df <- data.frame(player_id = p, date = d, games = g)
As output I require:
player_id date games type
1 1 2014-10-01 A A (OR NA)
2 1 2014-10-02 B Hybrid
3 1 2014-10-03 B B
4 2 2014-10-04 A A (OR NA)
5 2 2014-10-05 B Hybrid
6 2 2014-10-06 A Hybrid
7 6 2014-10-07 A A (OR NA)
8 6 2014-10-08 B Hybrid
9 6 2014-10-09 B B
The solution should be something like, apply
through the columns, and apply a function which checks back 30 days in time, and an ifelse()
statement to see what games they played.
This is a very similar post - and should help solve this problem. How do I do a conditional sum which only looks between certain date criteria
I have also explored, rowwise()
and conditional mutates()
using dplyr, however the catch is the historical time component for me.
Thanks for all the help! I can't thank this forum enough. I'll be checking back frequently.