# Compute frequencies for groups of variables

Stack Overflow Asked by Armel Tedjou on January 5, 2022

I want to compute the frequency of the modalities according to the species found.

Here is the data frame, and I want to count the number of each type _gite and count those where only aegypti was found, only those where albo where found and mixed where both were found together.

type_gite            aegypti          albopictus                    total
recipient_abandonne       19                   0                       19
recipient_stockage         0                   2                        2
recipient_stockage         8                   0                        8
recipient_stockage        36                   0                       36
recipient_stockage        13                   0                       13
recipient_stockage         1                   3                        4
autres                     0                   1                        1
autres                     0                   9                        9
recipient_abandonne        3                   0                        3

Here is how it should look like:

type gite             aegypti  albopictus  mixed  total
recipient_abandonne         2           0      0      2
recipient stockage          3           1      1      5
autres                      0           2      0      2
total                       5           3      1      9

Which code or aggregation formula is suited the most?

You can use dplyr and janitor (to get the Total row) to achieve what you need:

#install.packages("janitor")
#install.packages("dplyr")
library(dplyr)

df1 %>% select(-total_collected) %>% group_by(type_gite) %>%
mutate(mixed = +(aegyti_collected * albopictus_collected > 0)) %>%
mutate_at(vars(aegyti_collected:albopictus_collected), list(~+(. > 0)*!(mixed))) %>%
#>            type_gite aegyti_collected albopictus_collected mixed Total
#>               autres                0                    2     0     2
#>  recipient_abandonne                2                    0     0     2
#>   recipient_stockage                3                    1     1     5
#>                Total                5                    3     1     9

Data:

df1 <- structure(list(type_gite = structure(c(2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 2L),
.Label = c("autres", "recipient_abandonne", "recipient_stockage"),
class = "factor"),
aegyti_collected = c(19, 0, 8, 36, 13, 1, 0, 0, 3),
albopictus_collected = c(0, 2, 0, 0, 0, 3, 1, 9, 0),
total_collected = c(19, 2, 8, 36, 13, 4, 1, 9, 3)),
class = "data.frame", row.names = c(NA, -9L))

Created on 2019-04-30 by the reprex package (v0.2.1)

Answered by M-- on January 5, 2022

Here's what I came up with:

#create data
df = data.frame(type_gite = c('recipient_abandonne', 'recipient_stockage', 'recipient_stockage',
'recipient_stockage', 'recipient_stockage', 'recipient_stockage', 'autres', 'autres',
'recipient_abandonne'),
aegyti_collected = c(19, 0, 8, 36,13,1,0,0,3),
albopictus_collected = c(0,2,0,0,0,3,1,9,0),
total_collected = c(19,2,8,36,13,4,1,9,3))

#Classify as Mixed or only one of species using case when
df$label = case_when(df$albopictus_collected == 0 ~ 'Aegyti Only',
df$aegyti_collected == 0 ~ 'Albopictus Only', TRUE ~'Mixed') #frequency table df = data.frame(rbind(table(df$type_gite, df\$label)))

df = df %>% tibble::rownames_to_column(var = 'type_gite')

#create total column
library(janitor)

Answered by Kirsty Weitzel on January 5, 2022

I think you are looking for something like this. I took some random dummy data as an example.

library(dplyr)
# Create dummy data
df <- data.frame(matrix(rnorm(10), ncol = 2))
df <- cbind(c("blah", "blah", "meh", "meh", "meh"), df)
colnames(df) <- c("grouping_variable", "some_var", "some_other_var")
# Group by 1 variable & summarise on rest
df %>% group_by(grouping_variable) %>% summarise_all(sum)