【问题标题】:How to calculate proportions from multiple variables [duplicate]如何计算多个变量的比例[重复]
【发布时间】:2020-07-29 19:35:27
【问题描述】:

我希望根据多个变量从计数数据中计算比例。在下面的示例数据集中,我想知道每个物种在每个日期的每个分数的比例。例如,对于 2019-09-16 的所有 MCAP,2 的比例是多少?

structure(list(date = structure(c(18155, 18155, 18155, 18155, 
18155, 18155, 18155, 18155, 18171, 18171, 18171, 18185, 18185, 
18185, 18185, 18185, 18185, 18185, 18185, 18185), class = "Date"), 
species = c("MCAP", "MCAP", "PCOM", "MCAP", "MCAP", "MCAP", 
"PCOM", "PCOM", "PCOM", "PCOM", "PCOM", "MCAP", "MCAP", "MCAP", 
"MCAP", "PCOM", "PCOM", "PCOM", "PCOM", "PCOM"), score = c(2, 
2, 2, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3)), row.names = c(1L, 
2L, 3L, 4L, 8L, 33L, 37L, 38L, 7912L, 7931L, 7947L, 8543L, 8544L, 
8545L, 8547L, 12946L, 12947L, 12948L, 12949L, 12950L), class = "data.frame")

【问题讨论】:

  • 你能把已经失败的内容包括进来吗?这是你需要的:with(df, table(species,score))
  • 试试这个? df %>% group_by(species, score, date) %>% summarise(N = n()/ sum(n()))
  • @NelsonGon,这不起作用,因为它仍然按分数分组,所以所有比例都将是 1。您需要按所有人分组以获得计数,然后只按物种和日期得到比例。
  • @Matt D 您的方法首先按计数分组,然后按物种/日期分组。谢谢
  • @NelsonGon - 让我们使用示例 MCAP,2019-09-16。您的答案是按前 2 个实例以及分数进行分组(因此得分 2 的组和得分 3 的组)。现在您的解决方案计算这些组中的行数,然后除以所有行的总和 (sum(n()) 根据定义,这始终是一,因为您将所有数据分组,意味着 n() = sum(n()) df %>% group_by(species, score) %>% summarise(N = n()) - 运行它,你会看到它计算每个物种的 obs 数量和得分。与你的比较上面的代码。希望这会有所帮助。

标签: r


【解决方案1】:

tidyverse,特别是 dplyr 包可以帮助解决这个问题。我认为有很多方法可以使用这些包来解决这个问题,但这是我想到的第一个。

library(tidyverse)

data <- structure(list(date = structure(c(18155, 18155, 18155, 18155, 
                              18155, 18155, 18155, 18155, 18171, 18171, 18171, 18185, 18185, 
                              18185, 18185, 18185, 18185, 18185, 18185, 18185), class = "Date"), 
           species = c("MCAP", "MCAP", "PCOM", "MCAP", "MCAP", "MCAP", 
                       "PCOM", "PCOM", "PCOM", "PCOM", "PCOM", "MCAP", "MCAP", "MCAP", 
                       "MCAP", "PCOM", "PCOM", "PCOM", "PCOM", "PCOM"), score = c(2, 
                                                                                  2, 2, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3)), row.names = c(1L, 
                                                                                                                                                           2L, 3L, 4L, 8L, 33L, 37L, 38L, 7912L, 7931L, 7947L, 8543L, 8544L, 
                                                                                                                                                           8545L, 8547L, 12946L, 12947L, 12948L, 12949L, 12950L), class = "data.frame")

data_sum <- data %>%
  ##This groups and counts the occurrences
  group_by(species,date,score) %>% 
  tally() %>% 
  ungroup() %>% 
  ##this groups and calculates the proportion for the groups above without the score.
  group_by(species,date) %>% 
  mutate(prop = n/sum(n))

data_sum %>%
  filter(date == '2019-09-16',
         species == 'MCAP')

  species date       score     n  prop
  <chr>   <date>     <dbl> <int> <dbl>
1 MCAP    2019-09-16     2     3   0.6
2 MCAP    2019-09-16     3     2   0.4

【讨论】:

  • @Gregor - 这是正确的。我上面过滤的是他要求的例子。 2019-09-16 日期和 MCAP 物种。你上面的例子是物种 MCAP 和 2019-10-16,所以如果你看我下面的表格,你会看到每个分数的 50% 比例和与你上面所说的相匹配的计数。物种日期分数 n prop 1 MCAP 2019-09-16 2 3 0.6 2 MCAP 2019-09-16 3 2 0.4 3 MCAP 2019-10-16 2 2 0.5 4 MCAP 2019-10-16 3 2 0.5
【解决方案2】:

在基础 R 中,我们可以使用 tableprop.table 来做到这一点。您可以调整 margin 参数以更改比例的分母。代码很短,结果对于控制台中的表格显示相对较好,但不是很好,因为它不是用于合并到其他分析中的数据框。为此,我建议在另一个答案中使用 dplyr 方法。

with(d, prop.table(table(species, score, date), margin = c(1, 3)))
# , , date = 2019-09-16
# 
#        score
# species         2         3
#    MCAP 0.6000000 0.4000000
#    PCOM 0.3333333 0.6666667
# 
# , , date = 2019-10-02
# 
#        score
# species         2         3
#    MCAP                    
#    PCOM 0.3333333 0.6666667
# 
# , , date = 2019-10-16
# 
#        score
# species         2         3
#    MCAP 0.5000000 0.5000000
#    PCOM 0.0000000 1.0000000

【讨论】:

  • 这里margin有什么用?有没有办法让它自动化(比如从数据本身中获取)?
  • margin 指的是尺寸。在二维表中,边距 1 将是行,边距 2 将是列,所以如果你这样做,例如,prop.table(table(d$species, d$score), margin = 1) 行(物种)将加起来为 1。如果你这样做 margin = 2 列(得分) 将加起来为 1。在此示例的 3 维表中,OP 希望 datespecies 的比例加起来为 1。我的 c(1, 3)边距是因为species 是我的第一个参数,date 是我的第三个参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多