Home > Enterprise >  How to get R to count a value in a column if it meets a criteria in another column?
How to get R to count a value in a column if it meets a criteria in another column?

Time:01-19

let's say I have a dataset for number of tall and short students in each class.

Class tall_short count percentage
A     tall       2     17%       
A     short      3     20%       
B     tall       4     33%       
B     short      5     33%       
C     tall       6     50%       
C     short      7     47%       

I have the dataframe with the first 3 columns. How do I create the percentage column that shows the percentage of tall/short students in each class to all tall/short students.

CodePudding user response:

You can do

df %>% 
  group_by(tall_short) %>% 
  mutate(percentage = count / sum(count))

And use scales::label_percent to get the proper formatting:

df %>% 
  group_by(tall_short) %>% 
  mutate(percentage = scales::label_percent()(count / sum(count)))

  Class tall_short count percentage
  <chr> <chr>      <int> <chr>     
1 A     tall           2 17%       
2 A     short          3 20%       
3 B     tall           4 33%       
4 B     short          5 33%       
5 C     tall           6 50%       
6 C     short          7 47%       
  •  Tags:  
  • Related