简体   繁体   中英

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM