rvariablesmergefrequency-table

How to merge attributes on a frequency table in R?


Assume that i have two variables. See Dummy data below:

Out of 250 records:

SEX

Male : 100

Female : 150

HAIR

Short : 110

Long : 140

The code i currently use is provided below, For each variable a different table is created:

sexTable <- table(myDataSet$Sex)
hairTable <- table(myDataSet$Hair)

View(sexTable):
|------------------|------------------|
|       Level      |    Frequency     |
|------------------|------------------|
|        Male      |       100        |
|       Female     |       150        |
|------------------|------------------|


View(hairTable)
|------------------|------------------|
|       Level      |    Frequency     |
|------------------|------------------|
|        Short     |       110        |
|        Long      |       140        |
|------------------|------------------|

My question is how to merge the two tables in R that will have the following format As well as to calculate the percentage of frequency for each group of levels:

|---------------------|------------------|------------------|
|      Variables      |       Level      |    Frequency     |
|---------------------|------------------|------------------|
|      Sex(N=250)     |        Male      |       100 (40%)  |
|                     |       Female     |       150 (60%)  |
|      Hair(N=250)    |        Short     |       110 (44%)  |
|                     |        Long      |       140 (56%)  |
|---------------------|------------------|------------------|

Solution

  • We can use bind_rows after converting to data.frame

    library(dplyr)
    bind_rows(list(sex = as.data.frame(sexTable),
           Hair = as.data.frame(hairTable)), .id = 'Variables')
    

    Using a reproducible example

    tbl1 <- table(mtcars$cyl)
    tbl2 <- table(mtcars$vs)
    bind_rows(list(sex = as.data.frame(tbl1), 
           Hair = as.data.frame(tbl2)), .id = 'Variables')%>% 
       mutate(Variables = replace(Variables, duplicated(Variables), ""))
    

    If we also need the percentages

    dat1 <- transform(as.data.frame(tbl1), 
      Freq = sprintf('%d (%0.2f%%)', Freq,   as.numeric(prop.table(tbl1) * 100)))
    dat2 <- transform(as.data.frame(tbl2), 
      Freq = sprintf('%d (%0.2f%%)', Freq,   as.numeric(prop.table(tbl2) * 100)))
    bind_rows(list(sex = dat1, Hair = dat2, .id = 'Variables')