creates a data.frame counting occurencies of specific events in a given vector separated by named connex groups. For example it allows to count occurencies of each category of a qualitative variable above in the dataset for each individual. Grouping allows to reset the count each time another variable changes.
The idea is to create a cumulative history of what happened before current individual.
One new variable is created for each unique value of events
counting_events_group( events = events, group = NULL, base = NULL, prefix = NULL, suffix = NULL, clean = TRUE, keep = FALSE )
events | The vector of events to count (qualitative values). One new variable is created for each unique value of events. |
---|---|
group | qualitative vector that resets counting when its value changes. |
base | a dataframe to which counting can be added (cbind). |
prefix | (optional) prefix to the newly created variables names |
suffix | (optional) suffix to the newly created variables names |
clean | (boolean) to decide either or not variables names has to be cleaned (spaces, accents and special characters) |
keep | (boolean) to decide either or not events and group are to be added to the resulting data.frame |
a data.frame which number of rows is the length of events
events <- rep(c('a', 'b', 'c'), times = 20) group <- rep(rep(c('A', 'B'), c(12, 8)), times = 3) counting_events_group(events, group)#> a b c #> 1 0 0 0 #> 2 1 0 0 #> 3 1 1 0 #> 4 1 1 1 #> 5 2 1 1 #> 6 2 2 1 #> 7 2 2 2 #> 8 3 2 2 #> 9 3 3 2 #> 10 3 3 3 #> 11 4 3 3 #> 12 4 4 3 #> 13 0 0 0 #> 14 1 0 0 #> 15 1 1 0 #> 16 1 1 1 #> 17 2 1 1 #> 18 2 2 1 #> 19 2 2 2 #> 20 3 2 2 #> 21 0 0 0 #> 22 0 0 1 #> 23 1 0 1 #> 24 1 1 1 #> 25 1 1 2 #> 26 2 1 2 #> 27 2 2 2 #> 28 2 2 3 #> 29 3 2 3 #> 30 3 3 3 #> 31 3 3 4 #> 32 4 3 4 #> 33 0 0 0 #> 34 0 0 1 #> 35 1 0 1 #> 36 1 1 1 #> 37 1 1 2 #> 38 2 1 2 #> 39 2 2 2 #> 40 2 2 3 #> 41 0 0 0 #> 42 0 1 0 #> 43 0 1 1 #> 44 1 1 1 #> 45 1 2 1 #> 46 1 2 2 #> 47 2 2 2 #> 48 2 3 2 #> 49 2 3 3 #> 50 3 3 3 #> 51 3 4 3 #> 52 3 4 4 #> 53 0 0 0 #> 54 0 1 0 #> 55 0 1 1 #> 56 1 1 1 #> 57 1 2 1 #> 58 1 2 2 #> 59 2 2 2 #> 60 2 3 2