Calculate the difference betwen pairs of consecutive rows in a data frame - R

前端 未结 2 1928
一整个雨季
一整个雨季 2021-02-10 03:35

I have a data.frame in which each gene name is repeated and contains values for 2 conditions:

df <- data.frame(gene=c(\"A\",\"A\",\"B\",\"B\",\"C\",\"C\"),
c         


        
2条回答
  •  一向
    一向 (楼主)
    2021-02-10 04:18

    The plyr solution would look something like:

    library(plyr)
    reg.fun <- function(x) {
      reg.diff <- x$count[x$condition=='control'] - x$count[x$condition=='treatment']
      x$regulation <- ifelse(reg.diff > 0, 'up', 'down')
    
      x
    }
    
    ddply(df, .(gene), reg.fun)
    
    
      gene condition count  sd regulation
    1    A   control    10 1.0         up
    2    A treatment     2 0.2         up
    3    B   control     5 0.1       down
    4    B treatment     8 2.0       down
    5    C   control     5 0.8         up
    6    C treatment     1 0.1         up
    > 
    

    You could also think about doing this with a different package and/or with data in a different shape:

    df.w <- reshape(df, direction='wide', idvar='gene', timevar='condition')
    
    library(data.table)
    DT <- data.table(df.w, key='gene')
    
    DT[, regulation:=ifelse(count.control-count.treatment > 0, 'up', 'down'), by=gene]
    
       gene count.control sd.control count.treatment sd.treatment regulation
    1:    A            10        1.0               2          0.2         up
    2:    B             5        0.1               8          2.0       down
    3:    C             5        0.8               1          0.1         up
    >     
    

提交回复
热议问题