0

I have created a frequency table, DF, using the code below. However I would also like to create a column of percentages/proportions within the table, to see the percentage/proportion of each Function for each key. I am not sure how to adapt my code to do this. Any advice and help would be appreciated!

  gather(key = 'key', value = 'freq', -Function) %>%
  mutate(freq = as.numeric(freq)) %>% 
  group_by(Function, key) %>% 
  summarise(freq=sum(freq)) ``` 

B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
Jed
  • 331
  • 2
  • 11

2 Answers2

1

Try using this :

library(dplyr)
df %>%
  tidyr::gather(key = 'key', value = 'freq', -Function) %>%
  mutate(freq = as.numeric(freq)) %>% 
  group_by(key, Function) %>% 
  summarise(freq=sum(freq)) %>% #..... (1)
  mutate(freq = freq/sum(freq))

Note that -

  • gather has been retired, so use pivot_longer instead.
  • The above works without grouping by key explicitly because when you do summarise at (1) only last level of grouping is dropped i.e Function, so data is still grouped by key at (1).
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

If I understood your problem correctly, you can continue by grouping by key and the calculate the percentage/proportion

gather(key = 'key', value = 'freq', -Function) %>%
mutate(freq = as.numeric(freq)) %>% 
group_by(Function, key) %>% 
summarise(freq = sum(freq))  %>% 
group_by(key) %>%
mutate(prop = freq / sum(freq))
Pablo B.
  • 21
  • 3