根据组和条件划分2行值

时间:2019-06-04 21:17:50

标签: r

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

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

delegate

此代码将生成一个数据帧,如下所示:

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

3 个答案:

答案 0 :(得分: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 

答案 1 :(得分:0)

这里是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 

答案 2 :(得分:0)

带有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)]