# Counting and plotting combinations of survey 's replies

Hi,
I have got a question about counting of combinations of respondents' replies.
This is my df:

``````apartment_renovations <- structure(list(savings = c(1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1), bank_loans = c(
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 1, 1, 1, 0, 1
), relatives_loans = c(
1, 1, 0, 1, 1,
0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0
), instalments = c(
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0
), other = c(
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
)), class = c(
"tbl_df",
"tbl", "data.frame"
), row.names = c(NA, -31L))
``````

This is about getting money for apartment renovations, from savings, bank_loans, relatives_loans, instalments and other possibilities. My question is how to count all combinations of the above I mean: only savings, savings and relatives_loans, etc. I would like to make a plot of it if possible. Thank you for your ideas and help.

Maybe this can get you started. I encoded savings = 1, bank = 2, relatives = 4, so their sum can be from 0 to 7. I did a similar thing with installment and other, so their sum can be from 0 to 3. It is then easy to map the sums to combinations of categories, which I did by making factors. I plotted the combinations of savings+ bank + relatives on the x axis and used color to represent installments + other.

``````library(dplyr)
library(ggplot2)
apartment_renovations <- structure(list(savings = c(1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1), bank_loans = c(
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 1, 1, 1, 0, 1
), relatives_loans = c(
1, 1, 0, 1, 1,
0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0
), instalments = c(
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0
), other = c(
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
)), class = c(
"tbl_df",
"tbl", "data.frame"
), row.names = c(NA, -31L))

apartment_renovations <- apartment_renovations |>
mutate(Major=savings+(2*bank_loans)+(4*relatives_loans),
Minor=instalments+2*other)

apartment_renovations <- apartment_renovations |>
mutate(Major=factor(Major,
levels = 0:7,labels = c("None","S","B","S+B","R","S+R","B+R","S+B+R")),
Minor=factor(Minor,levels = 0:3,labels = c("neither","I","O","I+O")))

table(apartment_renovations\$Major,apartment_renovations\$Minor)
#>
#>         neither I O I+O
#>   None        0 0 0   0
#>   S           9 1 0   0
#>   B           0 1 0   0
#>   S+B         2 0 0   0
#>   R           5 0 1   0
#>   S+R         5 1 1   0
#>   B+R         1 0 0   0
#>   S+B+R       3 1 0   0

ggplot(apartment_renovations,aes(x=Major,fill=Minor))+
geom_bar(position = "dodge")
``````

Created on 2022-09-07 with reprex v2.0.2

Thank you very much @FJCC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.