【问题标题】:Calculate count and proportion of unique observations based on two variables根据两个变量计算唯一观察值的计数和比例
【发布时间】:2021-11-22 08:24:48
【问题描述】:

我有以下数据:

data <- structure(list(class = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 
                             3, 3, 3, 3, 3, 1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 
                             5), ID = c(700, 700, 800, 800, 800, 300, 300, 300, 300, 555, 
                                        555, 555, 555, 555, 555, 555, 555, 700, 700, 900, 900, 800, 300, 
                                        300, 300, 300, 555, 555, 555, 555, 555, 555, 555, 555), type = c(1, 
                                                                                                         1, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 
                                                                                                         3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1), date = structure(c(1610668800, 
                                                                                                                                                                 1610668800, 1610668800, 1610668800, 1610668800, 1610668800, 1610668800, 
                                                                                                                                                                 1610668800, 1610668800, 1610668800, 1610668800, 1610668800, 1610668800, 
                                                                                                                                                                 1610668800, 1610668800, 1610668800, 1610668800, 1610841600, 1610841600, 
                                                                                                                                                                 1610841600, 1610841600, 1610841600, 1610841600, 1610841600, 1610841600, 
                                                                                                                                                                 1610841600, 1610841600, 1610841600, 1610841600, 1610841600, 1610841600, 
                                                                                                                                                                 1610841600, 1610841600, 1610841600), class = c("POSIXct", "POSIXt"
                                                                                                                                                                 ), tzone = "UTC")), row.names = c(NA, -34L), class = c("tbl_df", 
                                                                                                                                                                                                                        "tbl", "data.frame"))

我想做的是计算每个日期和每个班级的 ID 列的计数/唯一次数,然后计算 1,2 和 3 的每种类型。例如,虽然 ID 700 出现了 2 次2021-01-15 我想贡献一次百分比。

我尝试了以下不同的变体,但没有成功:

data_perc <-  data %>%                                                                                                                                                                                                                         
 tabyl(class, type)

所以我的结果应该如下所示:

class     date     type1   type2   type3
  1    2021-01-15   30%     30%     40%
  1    2021-01-17   33%     33%     34%
  2    2021-01-15   60%     20%     20%   

提前谢谢你:)

【问题讨论】:

    标签: r dplyr percentage proportions


    【解决方案1】:

    不确定我是否误解了这个问题,或者预期的结果不是来自提供的样本,但无论如何这是我认为你想要的解决方案。

    data %>%
      group_by(class, date, type) %>%
      summarise(n = n_distinct(ID)) %>%
      group_by(class, date) %>% 
      mutate(
        n = n/sum(n),
        type = paste0("type", type),
        n = scales::percent(n) #optional row if want formatted as percent string
      ) %>%
      pivot_wider(
        names_from = type,
        values_from = n,
        values_fill = NA
      )
    

    【讨论】:

    • 嗨,不,这正是我要找的。非常感谢。我写这些结果只是为了说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多