reshape - r- cumulative frequency when every combination doesn't appear always -
I need to call everyday calls to the compounded client.
The example table is:
& gt; Data die clay llam elegios kalamam 1 1-11 a1 1 1 2 3-11 a 1 2 3 1-11 b 2 1 2 4 2-11 b 1 1 3 5 2-11 c 2 0 2
As you can see, the client did not call 2-11 in a day, so the combination client does not appear in the + 1/2 day table. If I run:
series & lt; -data.frame (dcast (data, elegios + diia ~ cumllam, length))
I get:
& gt; Series elegidos diameter X1 X2 X3 1 0 2-11 0 1 0 2 1 1-11 1 1 0 3 1 2-11 0 0 1 4 1 3-11 0 1 0
But If you call the second day how many clients have been called once, the customer should be seen and this is not because I have no line in the table for the combination table and in the previous table of Table 2-11.
The table should be visible:
elegidos diameter X1 X2 X3 1 0 2-11 0 1 0 2 1 1-11 1 1 0 3 1 2-11 1 0 1 4 1 3-11 0 1 1
x1 is the number of customers who were included in that right in the same call and that day.
The x2 is the number of customers who receive up to 2 calls in line as well as .
And so on.
The explanation is:
- Customer "One" receives a call at day 1 and 3, the client "B" calls 1 a day before 1 and the second day 2 Makes a call. So, on the first day we have 1 customer receives 1 call, and the other is receiving 2 calls
- On the second day, since it is cumulative, we are the customer who lives with the same call and client , Which receives one more call reaching 3 calls.
- On the third day, the customer gets another call and climb to 2 calls, that is why he is in X2 and the client remains the same in x3.
Is there a way to make this cumulative calculation every day, without creating a line for every day combination?
Thank you.
Try it out:
DT1 and LT; -Data [!! Data $ Egridos,] DAT 2 & lt; - Expand. Grid (DIA = sort (Exclusive (DAT1 $ Dia)), CLI = Exclusive (DAT1 dollar cli)) DT3 & lt; - Merge (Data, Dat2, all = TRUE) dat3n & lt; - dat3 [(with dat3, order (cli, dia)), library (zoo) dat3n [, c ('elegidos', 'cumllam']]
Update
You can also do this in the data.table
library (Data Eligible) DT <- data.table ( Data set (DT, Dia, CLI [D.E.] [Elygios, CJ (DIA = Exclusive (Dia), CLI = Unique (CLI)), DT [Elygios == 0, 1: 2, with = False ]) Nm1 & lt; c ('elegidos', 'cumllam') # is also a roll option but unfortunately I do not get it properly # So, I'm using na.locf from the zoo. 2 and lt; dt [dt1 [order (cd, dia)]], [nm1]: lapeli (sd, na.locf), sdcall = nm1] dcc.dttattable (dt2, elegios + dia ~ Cumllam, length, value.var = 'cumllam') # elegidos diameter 1 2 3 # 1: 0 2-11 0 1 0 # 2: 1 1-11 1 1 0 # 3: 1 2-11 1 0 1 # 4 : 1 3-11 0 1 1
data data & lt; - Structure (list (Diya = C ("1-11", "3-11", "1-11", "2-11", "2-11"), cli = c ("a", "a "," B "," b "," c "), llam = c (1l, 1l, 2l, 1l, 2l), elizios = c (1l, 1l, 1l, 1l , 0L), Kamalam = C (1L, 2L, 2L, 3L, 2L). Name = C ("square" = "data.frame", line.names = C ("1", "2", "3", "4", "Christian", "5"))
data & lt; - Structure (list (Diya = C ("1-11", "3-11", "1-11", "2-11", "2-11"), cli = c ("a", "a "," B "," b "," c "), llam = c (1l, 1l, 2l, 1l, 2l), elizios = c (1l, 1l, 1l, 1l , 0L), Kamalam = C (1L, 2L, 2L, 3L, 2L). Name = C ("square" = "data.frame", line.names = C ("1", "2", "3", "4", "Christian", "5"))
Comments
Post a Comment