I would like to calculate the percent of arrests on drugs for each age. using age_num and on_drugs(1 = on drugs, 0 = not on drugs). Im not really sure where to start, so would appreciate any help. I thought about using a while loop, but not sure if thats the best approach or not. Thanks for any help!
            Asked
            
        
        
            Active
            
        
            Viewed 631 times
        
    1 Answers
0
            I'd recommend using dplyr to group by and calculate summary measures. You can use the %>%(pipe) operator to chain operations together.
Then you could do something like:
library(dplyr)
drugs %>%
  group_by(age_num) %>%
  summarise(pct_on_drugs = mean(on_drugs) * 100)
(note: you don't provide a reproducible example, so this snippet is not tested).
 
    
    
        ajerneck
        
- 751
- 1
- 7
- 19
