【问题标题】:Summarize with mathematical conditions in dplyr用 dplyr 中的数学条件进行总结
【发布时间】:2020-03-30 15:05:00
【问题描述】:

基于这个问题:Summarize with conditions in dplyr 我想使用dplyr 根据数学条件总结一列(不是链接帖子中的字符串匹配)。我需要在measurement/time 的比率最高时找到最大measurement,同时创建一个新列ratio。我还想遍历整个行,我不确定如何使用dplyrsummarize 函数。


示例数据框

print(df)

   sample     type time measurement
1       a bacteria   24     0.57561
2       a bacteria   44     1.67236
3       a bacteria   67     4.17100
4       a bacteria   88    11.51661
5       b bacteria   24     0.53269
6       b bacteria   44     1.24942
7       b bacteria   67     5.72147
8       b bacteria   88    11.04017
9       c bacteria    0     0.00000
10      c bacteria   24     0.47418
11      c bacteria   39     1.06286
12      c bacteria   64     3.59649
13      c bacteria   78     7.05190
14      c bacteria  108     7.27060

期望的输出

  sample     type time measurement      ratio
1      a bacteria   88    11.51661 0.13087057
2      b bacteria   88    11.04017 0.12545648
3      c bacteria   78     7.05190 0.09040897

尝试失败

这仅返回group_bysummarize 函数定义的两列,希望将整个行信息带入:

library(dplyr)
df %>% 
    group_by(sample) %>%
    summarize(ratio = max(measurement/time, na.rm = TRUE))

  sample  ratio
  <fct>   <dbl>
1 a      0.131 
2 b      0.125 
3 c      0.0904

可重现的数据

structure(list(sample = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("a", "b", "c"), class = "factor"), 
    type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = "bacteria", class = "factor"), 
    time = c(24, 44, 67, 88, 24, 44, 67, 88, 0, 24, 39, 64, 78, 
    108), measurement = c(0.57561, 1.67236, 4.171, 11.51661, 
    0.53269, 1.24942, 5.72147, 11.04017, 0, 0.47418, 1.06286, 
    3.59649, 7.0519, 7.2706)), class = "data.frame", row.names = c(NA, 
-14L))

【问题讨论】:

  • 我不认为它确实如此。它很相似,但我有一个复合条件,那里的答案只是寻找最大值。
  • 我不确定您到底要做什么,因为在您的示例中,您采用的是比率的最大值。这不是你想要的条件吗?
  • 它非常相似,但我需要涉及使用多个列(而不仅仅是列中的单个值的最大值),差异很小。我明白为什么它会被标记为重复。但是,我是一位经验丰富的 SO/Google 员工,无法找到答案,因此其他人也可能会遇到困难。我提供了一个链接,对未来的 SO 用户很有帮助,以防他们遵循我的搜索模式

标签: r dplyr conditional-statements summarize


【解决方案1】:
df %>%
  mutate(ratio = measurement/time) %>%
  group_by(sample) %>%
  filter(ratio == max(ratio, na.rm=TRUE))

【讨论】:

    【解决方案2】:

    这应该可以解决问题。

    df %>%
       group_by(sample) %>%
       mutate(ratio = measurement/time) %>%
       filter(ratio == max(ratio)) 
    

    【讨论】:

    • 你为什么打电话给summarise?为什么不直接删除不想保留的列?
    • 说得好。事实上,没有必要删除列或总结任何内容。通过构造,每个样本只有一行。
    • 这会删除我的“c”样本。
    • 您有任何丢失的数据吗?如果是这样,传递max() na.rm = TRUE 参数
    【解决方案3】:

    一个选项是 filter 'measurement' 基于测量/时间的max 位置,并使用它来比较 (==) 与按 'sample' 分组后的 'measurement' 值

    library(dplyr)
    df %>%
       group_by(sample) %>% 
       mutate(ratio = measurement/time) %>%
       filter(measurement == measurement[which.max(ratio)])
    

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 2020-02-07
      • 2017-09-14
      • 2021-06-30
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      相关资源
      最近更新 更多