簡體   English   中英

有沒有辦法在R中划分group_by匯總統計的答案?

[英]Is there a way to divide answers of group_by summary statistics in R?

我正在嘗試從三個向量中對數據進行子集化,然后將算術應用於匯總統計數據,但我遇到了 count() 問題。 以下是使用 (summarise, dplyr) 的摘要,但我希望它以未過濾 (X_age65yr) 的百分比形式返回。
例如,阿拉巴馬州的篩選計數結果為 1667,總計數為 2411。我希望阿拉巴馬州和所有后續州按總數返回篩選計數,或 1667/2411 = .6914 或 69.14%

cthigh <- brfss2013 %>% filter(bphigh4 == "Yes", !is.na(X_age65yr),X_age65yr == "Age 65 or older") %>%
   group_by(X_state) %>% summarise(count = n())

cthigh
# A tibble: 53 x 2
   X_state              count
   <fct>                <int>
 1 Alabama               1667
 2 Alaska                 507
 3 Arizona                930
 4 Arkansas              1352
 5 California            1817
 6 Colorado              2302
 7 Connecticut           1488
 8 Delaware              1123
 9 District of Columbia  1032
10 Florida               8924
# ... with 43 more rows

ctall <- brfss2013 %>% filter(!is.na(X_age65yr),X_age65yr == "Age 65 or older") %>% 
    group_by(X_state) %>% summarise(count= n())

ctall
# A tibble: 53 x 2
   X_state              count
   <fct>                <int>
 1 Alabama               2411
 2 Alaska                 864
 3 Arizona               1578
 4 Arkansas              2069
 5 California            3111
 6 Colorado              4067
 7 Connecticut           2362
 8 Delaware              1786
 9 District of Columbia  1683
10 Florida              14245
# ... with 43 more rows

您可以計算bphigh4 == "Yes"的數量並將其除以每個X_state的行數以獲得比率。

library(dplyr)

brfss2013 %>% 
  filter(!is.na(X_age65yr) & !is.na(bphigh4),X_age65yr == "Age 65 or older") %>%
  group_by(X_state) %>% 
  summarise(count = sum(bphigh4 == "Yes")/n() * 100)

暫無
暫無

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

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