我有一個數據框,我想從兩列中輸出一個雙向列聯表。他們都有價值「太少」,「關於正確」或「太多」。R中的雙向列聯表
我打字
df %>%
filter(!is.na(col1)) %>%
group_by(col1) %>%
summarise(count = n())
分別爲二者的和得到的東西是這樣的:
col1 count
<fctr> <int>
Too Little 19259
About Right 9539
Too Much 2816
我想實現的是:
Too Little About Right Too Much Total
col1 19259 9539 2816 31614
col2 20619 9374 2262 32255
Total 39878 18913 5078 63869
我一直試圖使用表功能
addmargins(table(df$col1, df$col2))
但結果不是我想要
Too Little About Right Too Much Sum
Too Little 13770 4424 740 18934
About Right 4901 3706 700 9307
Too Much 1250 800 679 2729
Sum 19921 8930 2119 30970
那麼什麼是有望走出把你想要 – Wen
歡迎喜來所以,提問在[mimimal但完整的(很重要https://stackoverflow.com/help/ mcve)形式。也總是試圖包含一些示例數據(截至目前沒有人,但你可以看到'df')也許只有幾行就足以作爲一個簡單的例子 – Nate