r - Get percentage for cohort day and daycount -


i have cohort retention data frame

> cohortdata            cohort daycount count     1  25/10/2016        0   238     2  25/10/2016        1   137     3  25/10/2016        2    78     4  25/10/2016        3    32     5  25/10/2016        4    21     6  25/10/2016        5    25     7  26/10/2016        0   134     8  26/10/2016        1    97     9  26/10/2016        2    49     10 26/10/2016        3    22     11 26/10/2016        4    22     12 27/10/2016        0   136     13 27/10/2016        1    88     14 27/10/2016        2    38     15 27/10/2016        3    15     16 28/10/2016        0   138     17 28/10/2016        1    25     18 28/10/2016        2    19     19 29/10/2016        0   144     20 29/10/2016        1    28     21 30/10/2016        0   135 

what want add percent column % of count against each cohort , daycount of 0 cohort 25/10/2016 percentage values daycount 0 through 2 238/238, 137/238, 78/238.

i have looked @ table.prop not able result want, have tried doing cohortdata$count / (by=list(cohortdata$cohort, cohortdata$daycount==0)) not correct , gives errors.

i can convert data nxn matrix, 2nd matrix find % values , unlist , join data frame above sure there should simpler , elegant way go ><

a dplyr solution approach:

library(dplyr)  cohortdata %>%    group_by(cohort) %>%     mutate(percentage = count/count[daycount == 0]) #        cohort daycount count percentage #        <fctr>    <int> <int>      <dbl> #1  25/10/2016        0   238 1.00000000 #2  25/10/2016        1   137 0.57563025 #3  25/10/2016        2    78 0.32773109 #4  25/10/2016        3    32 0.13445378 #5  25/10/2016        4    21 0.08823529 #6  25/10/2016        5    25 0.10504202 #7  26/10/2016        0   134 1.00000000 #8  26/10/2016        1    97 0.72388060 #9  26/10/2016        2    49 0.36567164 #10 26/10/2016        3    22 0.16417910 ## ... 11 more rows 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -