Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 209899

R - Summarizing percentage by subgroups

$
0
0

I don't know how to explain my problem, but I want to summarize the categories distance and get the percentage for each distance per month. In my tabel 1 week is 100% and now I want to calculate the same for the month but using the percentage from the weeks. Something like sum(percent)/ amount of weeks in this month This is what I have:

  year  month year_week distance object_remarks        weeksum percent
 1 2017  05    2017_21   15       ctenolabrus_rupestris       3  0.75  
 2 2017  05    2017_21   10       ctenolabrus_rupestris       1  0.25  
 3 2017  05    2017_22   5        ctenolabrus_rupestris       5  0.833 
 4 2017  05    2017_22   0        ctenolabrus_rupestris       1  0.167
 5 2017  06    2017_22   0        ctenolabrus_rupestris       9  1   
 6 2017  06    2017_23   20       ctenolabrus_rupestris       6  0.545 
 7 2017  06    2017_23   0        ctenolabrus_rupestris       5  0.455 

I want to have an output like this:

  year  month  distance object_remarks        weeksum percent percent_month
 1 2017  05    15       ctenolabrus_rupestris       3  0.75           0.375
 2 2017  05    10       ctenolabrus_rupestris       1  0.25          0.1225
 3 2017  05    5        ctenolabrus_rupestris       5  0.833         0.4165 
 4 2017  05    0        ctenolabrus_rupestris       1  0.167         0.0835

 5 2017  06    0        ctenolabrus_rupestris       14 1.455         0.7275
 6 2017  06    20       ctenolabrus_rupestris       6  0.545         0.2775

Thanks a lot!


Viewing all articles
Browse latest Browse all 209899


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>