dimanche 2 juillet 2017

How to summarize data based on overlapping rows in R

I am posting this question as an extension to my previous question--How to subset data based on predefined pattern in R?.@ycw helped to solve the previous question. However, I have some more concerns to resolve. I need to aggregate the following data( this data the data of the part of solution in my previous question with an additional column).

MoveID  Travel_Time Person  Day.ID  Place   Location_Code   Conc_Code   Mode
NA          NA      5       1       0       H                   1       NA
0           0.5     5       1       1       C                   0       D
1           0.5     5       1       2       D                   0       D
2           0.5     5       1       3       W                   1       D
3           1       5       1       4       E                   0       T
4           1       5       1       5       W                   1       T
5           0.25    5       1       6       H                   1       D
6           0.5     5       1       7       F                   0       P
7           1       5       1       8       H                   1       P

I want to summarize the data based on Conc_Code in following way: Subset the data, start from first 1 to second 1-add Travel time for that subset and concatenate Mode (leaving the first cell in the subset). Then subset the data from second 1 to third 1-- add all cells in travel time in the subset, and concatenate cell for modes in the same subset leaving the first cell in the subset, so on for the third subset. The final data I am looking for looks like the following:

Person      Day     Journey Type        Mode/s  Travel Time(hr)
5           1       H-C-D-W             DDD     1.5
5           1       W-E-W               TT      2
5           1       W-H                 D       0.25
5           1       H-F-H               PP      1.5

I would appreciate if some could help me proper coding that I can use to get my result. Also, it is important for me to be able to replicate the result when the pattern of 1 and 0 in Conc_Code is different than the one in the sample data. Thanks!

Aucun commentaire:

Enregistrer un commentaire