【问题标题】:Combining multiple summary statistics in dplyr analysis在 dplyr 分析中结合多个汇总统计信息
【发布时间】:2018-08-22 13:48:11
【问题描述】:

对于示例数据框:

df1 <- structure(list(practice = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), drug = c("123A456", 
"123A567", "123A123", "123A567", "123A456", "123A123", "123A567", 
"123A567", "998A125", "123A456", "998A125", "123A567", "123A456", 
"998A125", "123A567", "123A567", "123A567", "998A125", "123A123", 
"998A125", "123A123", "123A456", "998A125", "123A567", "998A125", 
"123A456", "123A123", "998A125", "123A567", "123A567", "998A125", 
"123A456", "123A123", "123A567", "123A567", "998A125", "123A456"
), items = c(1, 2, 3, 4, 5, 4, 6, 7, 8, 9, 5, 6, 7, 8, 9, 4, 
5, 6, 3, 2, 3, 4, 5, 6, 7, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 4
), quantity = c(1, 2, 4, 5, 3, 2, 3, 5, 4, 5, 7, 9, 5, 3, 4, 
6, 1, 2, 4, 5, 3, 2, 3, 5, 4, 5, 7, 9, 5, 3, 4, 6, 1, 2, 4, 5, 
3)), .Names = c("practice", "drug", "items", "quantity"), row.names = c(NA, 
-37L), spec = structure(list(cols = structure(list(practice = structure(list(), class = c("collector_integer", 
"collector")), drug = structure(list(), class = c("collector_character", 
"collector")), items = structure(list(), class = c("collector_integer", 
"collector")), quantity = structure(list(), class = c("collector_integer", 
"collector"))), .Names = c("practice", "drug", "items", "quantity"
)), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"), class = c("tbl_df", 
"tbl", "data.frame"))

我想做各种分析。我认为 dplyr 将是我的解决方案,但我正在努力如何将功能组合在一起。

我的数据框是一个药物列表,我想总结其中一些药物(由药物代码的前三位数字定义)。

  1. 我想报告这些类型药物的总和(从 123 开始) - drug123.items 和 drug123.quantity BY practice。

  2. 我还想报告我的数据框中所有药物的所有药物(all_items 和 all_quantity)的总数(我最终会将 drug123 表示为所有药物的百分比)。

我可以单独进行一些分析,例如通过以下方式总结总项目:

practice <- df1 %>% 
  group_by(practice) %>% 
  summarise(all.items = sum(items))

...而且这里只看我感兴趣的药物...

drug123 <- df1 %>% 
  filter(substr(drug, 1,3)==123)


ALL.drug123 <- aggregate(drug123$quantity, by=list(Category=drug123$practice), FUN=sum)

但是我如何把所有东西放在一起呢?

我想要一个包含以下列的数据框:

练习(给定数据框中的 1,2,3)。

drug123.items #for drug123

drug123.quantity #for drug123

all.items #用于所有药物

all.quantity #适用于所有药物

有什么想法吗?

【问题讨论】:

  • 道歉@akrun...我还没有更新名称(现在完成)。
  • 你需要df1 %&gt;% group_by(practice) %&gt;% mutate(all.items = sum(items)) %&gt;% filter(str_sub(drug, 1, 3) == 123) %&gt;% summarise(all.items = first(all.items), quantSum = sum(quantity))
  • 嗨@akrun - 我想我愿意......我更愿意分别对每种药物(例如123)进行分析,并将所有药物的总量和项目作为额外的列所以我可以看到发生了什么......

标签: r dplyr plyr


【解决方案1】:

我想这就是你要找的东西:

df1 %>%
  group_by(practice) %>%
  summarize(items_123 = sum(if_else(stringr::str_detect(drug, '^123'), items, 0)),
            quantity_123 = sum(if_else(stringr::str_detect(drug, '^123'), quantity, 0)),
            all_items = sum(items),
            all_quantity = sum(quantity))

# A tibble: 3 x 5
  practice items_123 quantity_123 all_items all_quantity
     <int>     <dbl>        <dbl>     <dbl>        <dbl>
1        1        54           44        75           58
2        2        44           42        66           65
3        3        24           19        35           28

【讨论】:

  • 谢谢@zack - 看起来棒极了。我能问个问题吗?当我将代码应用于我的真实数据时出现错误......唯一的区别是我的药物代码名称很长 - BNFCode,'^0407020A0' - 这应该工作吗?我在 summarise_impl(.data, dots) 中收到错误错误:评估错误:false 必须是整数类型,而不是双精度。
  • 嗯,根据我的经验,它可能是 if_else 函数,它对真假子句的类型很严格。现在试试ifelse,看看能不能解决问题。
  • 也就是说 - 我的代码中的 itemquantity (以及提供的 dput 输出)是类型 double - 你的 itemsquantity列类型为integer?
  • 我使用 as.numeric 所以它们很好。你的建议虽然有效,但非常感谢。
猜你喜欢
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多