Subtraction within Groups using R

前端 未结 3 1942
盖世英雄少女心
盖世英雄少女心 2020-12-20 00:51

I have been struggling to find the best way to do this subtraction within groups. I have data frame containing a long list of samples (Sample), each one treate

相关标签:
3条回答
  • 2020-12-20 01:15

    Try this

    > transform(df, dValue=ave(value, Sample, FUN=function(x) c(0, x[1]-x[-1])))
      Sample condition value dValue
    1   var1         A    12      0
    2   var1         B    14     -2
    3   var1         C    15     -3
    4   var2         A    20      0
    5   var2         B    19      1
    6   var2         C    19      1
    7   var3         A    50      0
    8   var3         B    51     -1
    9   var3         C    48      2
    

    I'm assuming df looks like this

      Sample condition value
    1   var1         A    12
    2   var1         B    14
    3   var1         C    15
    4   var2         A    20
    5   var2         B    19
    6   var2         C    19
    7   var3         A    50
    8   var3         B    51
    9   var3         C    48
    
    0 讨论(0)
  • 2020-12-20 01:30

    You can also do that with dplyr:

    require(dplyr)
    
    df %.% 
       group_by(Sample) %.% 
       mutate(dValue = value[condition == "A"] - value)
    
    #  Sample condition value dValue
    #1   var1         A    12      0
    #2   var1         B    14     -2
    #3   var1         C    15     -3
    #4   var2         A    20      0
    #5   var2         B    19      1
    #6   var2         C    19      1
    #7   var3         A    50      0
    #8   var3         B    51     -1
    #9   var3         C    48      2
    
    0 讨论(0)
  • 2020-12-20 01:34

    Assuming your dataset is called dat, here's a couple of data.table solutions:

    Method 1:

    require(data.table) ## >= 1.9.2
    setDT(dat)[, dValue := value[condition == "A"] - value, by=Sample]
    

    Method 2:

    require(data.table) ## >= 1.9.2
    setkey(setDT(dat), Sample)
    dat[dat[condition == "A"], dValue := i.value-value]
    
    #    Sample condition value dValue
    # 1:   var1         A    12      0
    # 2:   var1         B    14     -2
    # 3:   var1         C    15     -3
    # 4:   var2         A    20      0
    # 5:   var2         B    19      1
    # 6:   var2         C    19      1
    # 7:   var3         A    50      0
    # 8:   var3         B    51     -1
    # 9:   var3         C    48      2
    
    0 讨论(0)
提交回复
热议问题