Two Way Frequency Table

Original source

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# Create some data
A <- c("yes", "no","yes", "no","yes", "no","yes", "no")
B <- c("male", "female","female", "male","male", "male","male", "male")
# A will be rows, B will be columns
mytable <- table(A,B)
# print table
mytable
     B
A     female male
  no       1    3
  yes      1    3
# A frequencies (summed over B)
margin.table(mytable, 1)
A
 no yes
  4   4
# B frequencies (summed over A)
margin.table(mytable, 2)
B
female   male
     2      6
# cell percentages
prop.table(mytable)
     B
A     female  male
  no   0.125 0.375
  yes  0.125 0.375
# row percentages
prop.table(mytable, 1)
     B
A     female male
  no    0.25 0.75
  yes   0.25 0.75
# column percentages
prop.table(mytable, 2)
     B
A     female male
  no     0.5  0.5
  yes    0.5  0.5