Thomas Matthew
Thomas Matthew

Reputation: 2886

R: Contingency Table from Dataframe

Using this sample data:

statedf <- data.frame(state.x77)
statedf$pop_class <- ifelse(statedf$Population > 10000, "so crowded!", 'where is everyone?')
statedf$frost_class <- ifelse(statedf$Frost > 100, "crampons", "flipflops")

How can I get a 2x2 table that shows the sum of all combinations of these binary variables? Eg how many states are "so crowded!" & "crampons", "so crowded!" & "t-shirt", 'where is everyone?' & "crampons", 'where is everyone?' & "t-shirt".

Upvotes: 0

Views: 166

Answers (1)

Thomas Matthew
Thomas Matthew

Reputation: 2886

use the table function:

table(statedf[,'pop_class'], statedf[,'frost_class'])

to get table of combinations:

                     crampons flipflops
  so crowded!               3         3
  where is everyone?       27        17

Upvotes: 1

Related Questions