1

I try to do a percentage by groups taking into account values from two different columns.

I have used dplyr in order to group my data and then to do the math operation. I am wrong to type the command cause the result is the same in all groups, I guess my code just shows one total percentage, and not by groups.

> x <- data.frame("code"=c("a","a","b","b","a","b"),"home" = c(1,1,1,1,1,1),
+                 "trap" = c(0,1,0,1,0,1))
> x
  code home trap
1    a    1    0
2    a    1    1
3    b    1    0
4    b    1    1
5    a    1    0
6    b    1    1
> x %>% 
+   group_by(code) %>% 
+   mutate(perc=(sum(trap)/sum(home)))
# A tibble: 6 x 4
# Groups:   code [2]
  code   home  trap  perc
  <fct> <dbl> <dbl> <dbl>
1 a         1     0   0.5
2 a         1     1   0.5
3 b         1     0   0.5
4 b         1     1   0.5
5 a         1     0   0.5
6 b         1     1   0.5
> 

the idea is obtain the weight of trap regarding home (which should be the same in all rows of the same group)

I want to get this dataframe:

  code   home  trap  perc
  <fct> <dbl> <dbl> <dbl>
1 a         1     0   0.333
2 a         1     1   0.333
3 b         1     0   0.666
4 b         1     1   0.666
5 a         1     0   0.333
6 b         1     1   0.666
> 

1 Answers1

0

We just need mean

x %>%
   group_by(code) %>% 
   dplyr::mutate(perc = mean(trap))
# A tibble: 6 x 4
# Groups:   code [2]
#  code   home  trap  perc
#  <fct> <dbl> <dbl> <dbl>
#1 a         1     0 0.333
#2 a         1     1 0.333
#3 b         1     0 0.667
#4 b         1     1 0.667
#5 a         1     0 0.333
#6 b         1     1 0.667

The different value may be due to summarise getting masked by plyr::summarise instead of dplyr::summarise (when both packages are loaded in the global env). To resolve this, either specify dplyr:: or start on a fresh session with only dplyr loaded

x %>% 
    group_by(code) %>%
    dplyr::mutate(perc = sum(trap)/sum(home))
akrun
  • 874,273
  • 37
  • 540
  • 662