【问题标题】:Proportions of character variables based on multiple variables and subgroups in RR中基于多个变量和子组的字符变量的比例
【发布时间】:2021-09-16 13:26:17
【问题描述】:

我在一个数据集中有两列代表会议

Occupation Gender
Director M
Director F
VP M
Director F
Secretary F
Director F

所以这里我们有 6 次会议,这是我想要获得的两个比例:

1 - 女性董事会议提案/所有会议 (3/6) 2 - 女性董事会议/女性出席会议的提案(3/4)

当然,我有成千上万的观察结果。

我尝试了这里给出的解决方案How to calculate proportions from multiple variables,但我没有得到我想要的结果。

我很确定这很容易,但我不知道如何考虑“如果”条件。另外,我想通过图表呈现结果,所以如果输出是数据框会更好。

【问题讨论】:

    标签: r dataframe proportions


    【解决方案1】:

    prop.table 将表格对象作为输入。这是你从没有指定保证金的电话中得到的(我认为这回答了你的第一个问题):

    prop.table(table(dat[[1]],dat[[2]])) 
    # could also be done on `table( dat$Occupation, dat$Gender)`
                  
                     F         M     
       Director   0.5000000 0.1666667
       Secretary  0.1666667 0.0000000
       VP         0.0000000 0.1666667
    

    您可能还需要行比例,通过使用 1 的 MARGIN(行的 R 约定)轻松获得。

    prop.table(table(dat[[1]],dat[[2]]), 1)
                 
                   F       M     
       Director      0.75    0.25
       Secretary     1.00    0.00
       VP            0.00    1.00
    

    #-----数据构造-----

    > TXT <- "
    + | Occupation| Gender|
    + | Director  | M     |
    + | Director  | F     |
    + | VP        | M     |
    + | Director  | F     |
    + | Secretary | F     |
    + | Director  | F     |"
    > dat <- read.table(text=TXT, head=T,sep='|')[-c(1,4)]
    > dat
       Occupation  Gender
    1  Director    M     
    2  Director    F     
    3  VP          M     
    4  Director    F     
    5  Secretary   F     
    6  Director    F     
    

    【讨论】:

    • table 需要一个数据框,所以这里table(dat) 可以。我认为proportionsprop.table 的当前名称(“早期名称,为向后兼容而保留”)。干杯
    • @Henrik:也许如果你有一个包或当前版本的 R 但我在 R 3.6 上,我得到?proportions No documentation for ‘proportions’ in specified packages and libraries: you could try ‘??proportions’
    • 好的。 proportions 是在 R 4.0.0 中引入的。 “新函数proportions()marginSums()。这些应该替换不幸命名的prop.table()margin.table()。它们是插入式替换,但也添加了命名边距功能。旧函数名称保留为back 的别名-兼容性。”
    • 我更新到 R 4.1.0,现在看到这个版本。现在我可以玩|&gt; &lt;g&gt;
    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2016-03-29
    • 2020-03-30
    • 2020-05-02
    • 2019-11-03
    • 1970-01-01
    相关资源
    最近更新 更多