'Is there a way to combine totals and frequencies across rows and columns in R

So I have a df with people putting down other people they've interacted with in covid time. And they can have multiple types of people (e.g. "colleague/s", "laborer/s", "Spouse") and then next to that have how many of that type of person (e.g. 4, 8, 100) if more than 1. So data would look like this:

contact1 multiple1  contact2  multiple2  contact3 multiple3 ...
Spouse      0        Children   3        Neighbor    0 
Children    2        Aunt       0        Colleagues  5
Parents     2        Children   4        Colleagues  10  

Is there a way to combine all the contacts and multiples to get the total number of Children/ Colleagues/etc for all people and types of contacts instead of just the frequency of times a type of contact comes up?

So I want for example:

ContactType   Total Multiple 
Spouse              1
Children            9
Colleagues          15 
Neighbor            1 ....
r


Solution 1:[1]

There maybe a better to do this, below is my approach

df<- data.frame(contact1 = c('spouse', 'children', 'parents'), 
     multiple1 = c(0,2,2), contact2 = c('children', 'aunt', 'children'),           
     multiple2 = c(3, 0, 4), contact3 = c('neighbor','colleagues','colleagues'), 
     multiple3  = c(0, 5, 10))

df2 <- df %>% select(matches('contact'))

df3 <- df %>% select(matches('multiple'))

Once we have df2 and df3, we can do

cbind(df2 %>% 
mutate(row = row_number()) %>% 
melt(id.var = 'row') %>% 
rename(contact_type = value) %>% 
select(-variable), 
df3 %>% mutate(row = row_number()) %>% 
melt(id.var = 'row') %>% 
rename(total_multiple = value) %>% 
select(-variable, -row)) %>% 
group_by(contact_type) %>% 
summarise(total_multiple =  ifelse( n() == 1, 1,  sum(total_multiple)))

Here is the output

# A tibble: 6 x 2
  contact_type total_multiple
  <chr>                 <dbl>
1 aunt                      1
2 children                  9
3 colleagues               15
4 neighbor                  1
5 parents                   1
6 spouse                    1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vendetta