I want to ask how can I merge Tibbles.
I am trying to do a table for sociodemographics.
Sample data frame:
education <- c("high school", "elementary school", NA, "university", "high school")
relationship <- c("single", "married", "divorced", NA, "single")
occupation <- c("student", "teacher", "unemployed", NA, "doctor")
df <- data.frame(education, relationship, occupation)
df
education relationship occupation
1 high school single student
2 elementary school married teacher
3 <NA> divorced unemployed
4 university <NA> <NA>
5 high school single doctor
I want to calculate the sample size and percentages for each variable and merge them on the "n" and percentages column.
I did "n" and percentage calculations like this:
library(dplyr)
library(formattable)
a <- df %>%filter(!is.na(education))%>%
group_by(education) %>%
summarise(n = n()) %>%
mutate(`%` = formattable::percent(n / sum(n)))
a
A tibble: 3 x 3
education n `%`
<chr> <int> <formttbl>
1 elementary school 1 25.00%
2 high school 2 50.00%
3 university 1 25.00%
b <- df %>%filter(!is.na(relationship))%>%
group_by(relationship) %>%
summarise(n = n()) %>%
mutate(`%` = formattable::percent(n / sum(n)))
b
A tibble: 3 x 3
relationship n `%`
<chr> <int> <formttbl>
1 divorced 1 25.00%
2 married 1 25.00%
3 single 2 50.00%
c <- df %>%filter(!is.na(occupation))%>%
group_by(occupation) %>%
summarise(n = n()) %>%
mutate(`%` = formattable::percent(n / sum(n)))
c
A tibble: 4 x 3
occupation n `%`
<chr> <int> <formttbl>
1 doctor 1 25.00%
2 student 1 25.00%
3 teacher 1 25.00%
4 unemployed 1 25.00%
My question is how can I merge them as one below another by the "n" and "percentage" columns and have one big sociodemographics table?