简体   繁体   English

R table() 函数; 如何获取列名?

[英]R table() function; how do I get column names included?

Say I have a dataframe with df$Cat, df$IsYellow.假设我有一个带有 df$Cat、df$IsYellow 的数据框。 Both columns can either be "Yes" or "No"两列都可以是“是”或“否”

How do I generate the 2x2 table and also have Cat and IsYellow printed on the table so that I know which side of the table belongs to Cat and which side belongs to IsYellow?如何生成 2x2 表并在表上打印 Cat 和 IsYellow,以便我知道表的哪一侧属于 Cat,哪一侧属于 IsYellow?

df <- data.frame(Cat=sample(c('Yes', 'No'), size=10, replace=TRUE),
                 IsYellow=sample(c('Yes', 'No'), size=10, replace=TRUE))
table(df)
##      IsYellow
## Cat   No Yes
##   No   3   1
##   Yes  2   4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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