Ping Jung
Ping Jung

Reputation: 1

R: how to find the ratio when using dplyr

I want to add a column to find ratio of the element which divide by the total of elements that shared same type, for example, (type,genre)=(1,0),the ratio will be n/sum(same type)=2/3

coco<-data.frame(type=c(1,2,1,2,3,1,2,3,4,4),genre=c(0,1,0,1,1,1,0,0,1,0))
  
coco%>%group_by(type,genre)%>%summarise(n=n())

 # A tibble: 8 x 3
# Groups:   type [4]
  type genre     n
    <dbl> <dbl> <int>
1     1     0     2
2     1     1     1
3     2     0     1
4     2     1     2
5     3     0     1
6     3     1     1
7     4     0     1
8     4     1     1

coco%>%count(type)
  type n
1    1 3
2    2 3
3    3 2
4    4 2

I tried to use:

 coco%>%group_by(type,genre)%>%summarise(n=n(),ratio=n/sum(type))

but didn't work, it should print out like:

    type genre     n  ratio
    <dbl> <dbl> <int>
1     1     0     2    0.66
2     1     1     1    0.33
3     2     0     1    0.33
4     2     1     2    0.66
5     3     0     1    0.5
6     3     1     1    0.5
7     4     0     1    0.5
8     4     1     1    0.5

May I ask what part should I modify? (Sorry for bad explanation and thank in advance)

Upvotes: 0

Views: 404

Answers (3)

Macosso
Macosso

Reputation: 1439

You need to divide n with the count sum(n) to get you desired results because the data was not grouped by type only

kind check my code

coco %>% group_by(type,genre) %>%
  summarise(n=n(), ) %>%
  mutate(ratio = n/sum(n))

Upvotes: 1

Anoushiravan R
Anoushiravan R

Reputation: 21908

I hope I got what you have in mind right:

coco %>%
  add_count(type) %>%
  arrange(type) %>%
  group_by(genre, type) %>%
  mutate(avg = n() / n)

# A tibble: 10 x 4
# Groups:   genre, type [8]
    type genre     n   avg
   <dbl> <dbl> <int> <dbl>
 1     1     0     3 0.667
 2     1     0     3 0.667
 3     1     1     3 0.333
 4     2     1     3 0.667
 5     2     1     3 0.667
 6     2     0     3 0.333
 7     3     1     2 0.5  
 8     3     0     2 0.5  
 9     4     1     2 0.5  
10     4     0     2 0.5 

Upvotes: 0

Jon Spring
Jon Spring

Reputation: 66445

A shortcut for group_by(x) %>% summarize(n = n()) is count(x).

Your code would work if you modified to

coco%>%group_by(type,genre)%>%summarise(n=n()) %>% mutate(ratio=n/sum(n))

The summarise line leaves the type grouping intact, at which point you can feed that into mutate where you compare that n to the total n for that group of type.

Here's another way, which I slightly prefer since the type grouping is written explicitly. (I have made mistakes before by not realizing what level of grouping remained after a group_by - summarize...)

coco %>%
  count(type, genre) %>%
  group_by(type) %>%
  mutate(ratio = n / sum(n)) %>%
  ungroup()

# A tibble: 8 x 4
   type genre     n ratio
  <dbl> <dbl> <int> <dbl>
1     1     0     2 0.667
2     1     1     1 0.333
3     2     0     1 0.333
4     2     1     2 0.667
5     3     0     1 0.5  
6     3     1     1 0.5  
7     4     0     1 0.5  
8     4     1     1 0.5  

Upvotes: 3

Related Questions