0

I have a data source that looks like the following:

pirate_size   pirate_age   victim_size
large         adult        large
large         adult        large
large         immature     small
small         immature     small
small         immature     small

I want to create a contingency table in R that contains the values divided by the total number of rows (in this case, the total number is 5). Using the following code I get a normal contingency table:

 table1 <-(data$pirate_age,data$pirate_size)

But I want the output to be:

           Adult           Immature

Large      2/5             1/5

Small      0               2/5
HRDSL
  • 711
  • 1
  • 5
  • 22

3 Answers3

1

Divide by number of rows after using table

table(df$pirate_size, df$pirate_age)/nrow(df)

#        adult immature
#  large   0.4      0.2
#  small   0.0      0.4

data

df <- structure(list(pirate_size = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("large", 
"small"), class = "factor"), pirate_age = structure(c(1L, 1L, 
2L, 2L, 2L), .Label = c("adult", "immature"), class = "factor"), 
victim_size = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("large", 
"small"), class = "factor")), class = "data.frame", row.names = c(NA, -5L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

Can also use prop.table() and reformat as %:

prop.table(table(df$pirate_size, df$pirate_age))*100

        adult immature
  large    40       20
  small     0       40
Corey Pembleton
  • 717
  • 9
  • 23
0

One option is to use dcast to cast to long with a custom function for fun.aggeregate. Note: This will give a data frame rather than an object of class "table"

library(data.table)

dcast(df, pirate_size ~ pirate_age, value.var = 'pirate_age',
      fun.aggregate = function(x) length(x)/nrow(df))

#   pirate_size adult immature
# 1       large   0.4      0.2
# 2       small   0.0      0.4
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38