How to create two columns that count the total number of two conditions

I have a diabetes dataset that has a column called Outcome and only has two values, 1 = Diabetes, 0 = Non-Diabetes. I want to count the total number of 1's and 0's based on age and then have a % of 1's based on age.

I have this code below:

by_age1 <- 
  diabetes.df %>%
  select(Age, Outcome) %>%
  group_by(Age,Outcome) %>%
  summarize(Diabetes_Count = n()) %>%
  filter(Outcome=="1"| Outcome == "0")

This code generates this table

Age | Outcome | Count
21      0        58
21      1         5    

And so on

I want the table to look like this though

Age | Count_Outcome=1 | Count_Outcome=0
21          5                 58
22          11                61

So I can eventually get to this

Age | Count_Outcome=1 | Count_Outcome=0 | Count_Outcome=1/Count_Outcome=0
21          5                 58                    0.086
22          11                61                    0.180



Read more here: https://stackoverflow.com/questions/67387847/how-to-create-two-columns-that-count-the-total-number-of-two-conditions

Content Attribution

This content was originally published by Conner McCoy at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: