1

I'm a new R programmer with what is probably a very simple issue.

I have a dplyr summarize/spread snippet as follows:

enroll_data_eth_sum <- group_by(enroll_data_distinct, EMISEthnicity) %>% 
  summarize(freq = n()
  ) %>% 
  spread(EMISEthnicity, freq)

Which produces this table:

Output Table

What I need is that table to be percentages of the row...not the values. I have tried a few things (like freq = (n=n()) / sum(n)), but this just gave all 1s (which upon reflection was probably correct).

Any thoughts? I know it's something simple...thank you!!

John

UPDATE: I tried the following based on Cedric's post:

enroll_data_eth_sum <- group_by(enroll_data_distinct, EMISEthnicity) 
 %>% 
 summarize(n = n()) %>% 
 mutate(freq = n / sum(n)) %>% 
 spread(EMISEthnicity, freq)

But that gave me this table:

Updated with Mutate

The values are correct...but I need it all on one row. ??

River
  • 8,585
  • 14
  • 54
  • 67
  • 1
    Possible duplicate of [Relative frequencies / proportions with dplyr](https://stackoverflow.com/questions/24576515/relative-frequencies-proportions-with-dplyr) – Cedric Nov 12 '17 at 13:06
  • Hi Cedric, I tried that...but that mtcars code gave me 7 rows (one for each summary total of Ethnicity) and 8 columns (one for each Ethnicity). I just need it all on one row...like the Excel "% of Row" calculation. – John LaPlante Nov 12 '17 at 13:59
  • Sorry, it's good you got the right answer then ! – Cedric Nov 12 '17 at 20:34
  • @JohnLaPlante if gatsky's answer helped, consider accepting it as an answer by clicking on the check mark to the left of the answer. – CPak Nov 12 '17 at 21:49

1 Answers1

1

Just get rid of the n column, eg:

enroll_data_eth_sum <- group_by(enroll_data_distinct, EMISEthnicity) %>% 
 summarize(n = n()) %>% 
 mutate(freq = n / sum(n)) %>%
 select(-n) %>%
 spread(EMISEthnicity, freq)
gatsky
  • 1,180
  • 7
  • 8