I have an R data frame that looks like this:
User | request_id | previous_request_id
-------------------------------------
A | 9 | 5
A | 3 | 1
A | 5 | NA
A | 1 | 9
B | 2 | 8
B | 8 | 7
B | 7 | NA
B | 4 | 2
Each row corresponds to a request made by a specific user. Each row has a user identifier, request identifier, and identifier of their previous request. If there is no previous request, the value NA is specified in the previous_request_id field.
For each user, I want to order each request using the previous request identifier using:
- The order is 1 if previous_request_id is NA
- The order is 2 if previous_request_id is request_id with order 1
- Order is 3 if previous_request_id is request_id with order 2
- and etc.
The result of the above rules applied to the first table should look like this:
User | request_id | previous_request_id | Order
---------------------------------------------
A | 9 | 5 | 2
A | 3 | 1 | 4
A | 5 | NA | 1
A | 1 | 9 | 3
B | 2 | 8 | 3
B | 8 | 7 | 2
B | 7 | NA | 1
B | 4 | 2 | 4
Is there any way to do this inside R? I believe that a graphic database package may be a way to do this, but so far I have not been able to find anything in my research (in the center in Cypher Neo4j).
Any help here would be greatly appreciated!
source share