0

Here is a data frame I have and I want to compute the breteau index

## Here is the table

commune container   house   aegypti albopictus
yde4       c1            h1    6        6
yde2       c2            h2    2        3
yde7       c3            h3    1        0
yde7       c3            h4    1        1
yde7       c5            h5    8        0
yde7       c6            h6    0        0
yde4       c7            h7    4        1
yde7       c8            h8    14       9
yde3       c9            h9    0        1
yde3       c10           h10   6        2

## here is how it should display

                                        aegypti                albopictus   
com    house_pros     c_found   Pos_container     In    Pos_container   In
yde2    1              1             1            100          1        100     
yde3    2              2             1             50          2        100
yde4    2              2             2            100          2        100
yde7    4              5             4            100          2        40
Total   9              10            8             88          7        70

`com = commune, house_pros = number of house prospected, c_found = the number of container found in each commune, pos_container = the number of positive container (container where at least one either aegypti or albopictus was found) per commune and for each species (aegypti or albopictus) (taking into account that we may have more than one container in a house), and In = Breteau index computed ((number of positive container / number of house prospected)*100. The index is computed for each species and for each commune. And totals row and column are added.

I couldn't be able to find out the suitable code.

Could some one help me with the right one?

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59

1 Answers1

0

Here is one approach using dplyr, first compute house_pros, c_found and aegypti_Pos_container using summarize then append row at the end for Total using bind_rows. Use the same steps for albopictus

library(dplyr)
df %>% group_by(commune) %>% 
       summarise(house_pros=n_distinct(container),    #See dplyr::n_distinct
                 c_found=n(),                         # Size of each group
                 aegypti_Pos_container=sum(aegypti!=0)) %>%  #Num of aegypti !=0
       bind_rows(.,tibble(commune='Total',house_pros=sum(.$house_pros),c_found=sum(.$c_found),
                          aegypti_Pos_container=sum(.$aegypti_Pos_container))) %>% 
       mutate(aegypti_In=(aegypti_Pos_container/house_pros)*100)


# A tibble: 5 x 5
  commune house_pros c_found aegypti_Pos_container aegypti_In
  <chr>        <int>   <int>                 <int>      <dbl>
1 yde2             1       1                     1      100  
2 yde3             2       2                     1       50  
3 yde4             2       2                     2      100  
4 yde7             4       5                     4      100  
5 Total            9      10                     8       88.9
A. Suliman
  • 12,923
  • 5
  • 24
  • 37