簡體   English   中英

如何通過 dplyr 在 R 上生成頻率表並用 ggplot 繪制其值?

[英]How do I generate a frequency table on R via dplyr and plot its values with ggplot?

我需要從兩個分類變量列中做一個頻率表,其中一個是 5 歲年齡組,另一個是來自 brfss2013 數據集的健康狀況(五個州),我從中提取了感興趣的列:

> hlthgrpq1 <- brfss2013 %>% select(genhlth, X_ageg5yr)

因此生成了一個兩列框架,2 個變量的 491775 個觀察值。

    genhlth     X_ageg5yr
1   Fair        Age 60 to 64
2   Good        Age 50 to 54
3   Good        Age 55 to 59
4   Very good   Age 60 to 64
5   Good        Age 65 to 69

我可以使用 'by' 函數生成一個匯總表:

> by(hlthgrpq1$genhlth, hlthgrpq1$X_ageg5yr, summary)
hlthgrpq1$X_ageg5yr: Age 18 to 24
Excellent Very good      Good      Fair      Poor      NA's 
     6896     10266      7795      1873       303        69 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 25 to 29
Excellent Very good      Good      Fair      Poor      NA's 
     5779      8488      6521      1751       325        46 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 30 to 34
Excellent Very good      Good      Fair      Poor      NA's 
     6412      9958      7977      2295       496        75 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 35 to 39
Excellent Very good      Good      Fair      Poor      NA's 
     6366     10169      8236      2637       638        61 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 40 to 44
Excellent Very good      Good      Fair      Poor      NA's 
     6689     11130      9193      3334      1067        95 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 45 to 49
Excellent Very good      Good      Fair      Poor      NA's 
     7051     12278     10611      4343      1815       112 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 50 to 54
Excellent Very good      Good      Fair      Poor      NA's 
     8545     15254     13761      6354      3120       139 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 55 to 59
Excellent Very good      Good      Fair      Poor      NA's 
     8500     16759     15394      7643      3998       197 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 60 to 64
Excellent Very good      Good      Fair      Poor      NA's 
     8283     16825     16266      8101      3955       229 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 65 to 69
Excellent Very good      Good      Fair      Poor      NA's 
     7479     15764     15600      7749      3200       205 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 70 to 74
Excellent Very good      Good      Fair      Poor      NA's 
     5491     11943     13125      6491      2721       196 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 75 to 79
Excellent Very good      Good      Fair      Poor      NA's 
     3320      8501     10128      5545      2426       173 
---------------------------------------------------------------------------------------------------------------- 
hlthgrpq1$X_ageg5yr: Age 80 or older
Excellent Very good      Good      Fair      Poor      NA's 
     3697     10285     14400      8116      3695       322 

這就是我陷入困境的地方。 我已經嘗試了幾個小時試圖到達這里:

通過電子表格獲得的結果。

謝謝你的幫助。

(這是針對特定任務的,因此我只能使用 dplyr 和 ggplot2,因此,沒有 reshape2 或 tidyr。)

首先:對於將來的發布,最好始終包含示例數據。 請參閱此處如何提供包含樣本數據的最小可重現示例/嘗試

基礎 R 中的解決方案。

as.data.frame.matrix(t(table(df)));
#             Fair Good Very good
#Age 50 to 54    0    1         0
#Age 55 to 59    0    1         0
#Age 60 to 64    1    0         1
#Age 65 to 69    0    1         0

或者像這樣的tidyverse方法?

library(tidyverse);
df %>% count(genhlth, X_ageg5yr) %>% spread(genhlth, n);
## A tibble: 4 x 4
#  X_ageg5yr     Fair  Good `Very good`
#  <fct>        <int> <int>       <int>
#1 Age 50 to 54    NA     1          NA
#2 Age 55 to 59    NA     1          NA
#3 Age 60 to 64     1    NA           1
#4 Age 65 to 69    NA     1          NA

或者,如果您堅持只使用dplyr而不是tidyr ,您可以這樣做:

df2 <- df %>%
    count(genhlth, X_ageg5yr);
df2 <- as.data.frame.matrix(xtabs(n ~ X_ageg5yr + genhlth, data = df2));
#             Fair Good Very good
#Age 50 to 54    0    1         0
#Age 55 to 59    0    1         0
#Age 60 to 64    1    0         1
#Age 65 to 69    0    1         0

這基本上歸結為從寬到長的重新格式化,因此圍繞該主題進行了大量討論(例如here )。


樣本數據

df <- read.table(text =
    "genhlth     X_ageg5yr
Fair        'Age 60 to 64'
Good        'Age 50 to 54'
Good        'Age 55 to 59'
'Very good'   'Age 60 to 64'
Good        'Age 65 to 69'", header = T)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM