【问题标题】:Division of 2 row values based on group and condition根据组和条件划分 2 行值
【发布时间】:2019-10-20 10:22:00
【问题描述】:

我想必须将 2 行的第 4 列(“计数”)中的值相除,其中第一列具有相同的值。

我尝试使用“group_by”,但无法告诉 R 我想将 FAST Count 值除以 SLOW Count 值。换句话说,我不知道如何单独访问这些值。

cells <- rep(c("CELL1","CELL2"), times =2 , each = 4)
temps <- rep(c(10,20,30,40), times = 4)
corners <- rep(c("FAST","SLOW"), times = 1, each = 8)
counts <- c(3200,5000,7250,10000,150,250,400,600,2400,12000,3600,2100,50,80,120,180)

df <- data.frame(cells,temps,corners,counts)

此代码将生成如下所示的数据框:

    cells temps corners counts
    1  CELL1    10    FAST   3200
    2  CELL1    20    FAST   5000
    3  CELL1    30    FAST   7250
    4  CELL1    40    FAST  10000
    5  CELL2    10    FAST    150
    6  CELL2    20    FAST    250
    7  CELL2    30    FAST    400
    8  CELL2    40    FAST    600
    9  CELL1    10    SLOW   2400
    10 CELL1    20    SLOW  12000
    11 CELL1    30    SLOW   3600
    12 CELL1    40    SLOW   2100
    13 CELL2    10    SLOW     50
    14 CELL2    20    SLOW     80
    15 CELL2    30    SLOW    120
    16 CELL2    40    SLOW    180

预期输出是一个新的数据框,其中包含单元格、临时和除法结果:

       cells temps   div_result
    1  CELL1    10     1.33
    2  CELL1    20     0.42
    3  CELL1    30     2.01
    4  CELL1    40     4.55
    5  CELL2    10     3.00
    6  CELL2    20     3.13
    7  CELL2    30     3.33
    8  CELL2    40     3.33

【问题讨论】:

  • aggregate(counts ~ cells + temps, df, FUN = function(x) x[1] / x[2])(您需要重新排序结果)

标签: r


【解决方案1】:

涉及dplyr 的一种可能性是:

df %>%
 group_by(cells, temps) %>%
 summarise(div_result = first(counts)/last(counts))

  cells temps div_result
  <fct> <dbl>      <dbl>
1 CELL1    10      1.33 
2 CELL1    20      0.417
3 CELL1    30      2.01 
4 CELL1    40      4.76 
5 CELL2    10      3    
6 CELL2    20      3.12 
7 CELL2    30      3.33 
8 CELL2    40      3.33 

【讨论】:

    【解决方案2】:

    这是dplyr的一种方式-

    df %>% 
      group_by(cells, temps) %>% 
      summarize(
        div_result = counts[corners == "FAST"]/counts[corners == "SLOW"]
      ) %>% 
      ungroup()
    
    # A tibble: 8 x 3
      cells temps div_result
      <fct> <dbl>      <dbl>
    1 CELL1    10      1.33 
    2 CELL1    20      0.417
    3 CELL1    30      2.01 
    4 CELL1    40      4.76 
    5 CELL2    10      3    
    6 CELL2    20      3.12 
    7 CELL2    30      3.33 
    8 CELL2    40      3.33 
    

    【讨论】:

      【解决方案3】:

      base R 的选项

      by(df[['counts']], df[c('cells', 'temps')], FUN = function(x) x[1]/x[2])
      

      data.table

      library(data.table)
      setDT(df)[, .(div_results = first(counts)/last(counts)), .(cells, temps)]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-06
        • 1970-01-01
        • 1970-01-01
        • 2016-07-09
        • 1970-01-01
        • 1970-01-01
        • 2023-02-08
        • 2021-08-31
        相关资源
        最近更新 更多