Greg Owens Greg Owens - 1 month ago 6
R Question

R dplyr sum based on conditions

I'm trying to use dplyr to multiply and sum one column, based on variables in other columns.

location = c("LBJ", "LBJ", "LBJ","LBJ")
sample = c("100", "100", "100","100")
sum = c(0,1,2,3)
n = c(200,100,20,24)
df = data.frame(location, sample, sum,n)
df
location sample sum n
1 LBJ 100 0 200
2 LBJ 100 1 100
3 LBJ 100 2 20
4 LBJ 100 3 24


I would like to calculate ( (n where sum == 0) + ((n where sum == 1) / 2 ) ) / (sum of all n).

I am going to have multiple locations and samples which should act independently, so I want to use the group_by commands in dplyr.

Thanks for any help.

Wen Wen
Answer Source

Is this what you want ?

library(dplyr)

df%>%group_by(location)%>%dplyr::mutate(Rate=mean(n[which(sum<=1)])/sum(n))

# A tibble: 4 x 5
# Groups:   location [1]
  location sample   sum     n      Rate
    <fctr> <fctr> <dbl> <dbl>     <dbl>
1      LBJ    100     0   200 0.4360465
2      LBJ    100     1   100 0.4360465
3      LBJ    100     2    20 0.4360465
4      LBJ    100     3    24 0.4360465