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

前端 未结 2 1927
一整个雨季
一整个雨季 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
    >     
    
    0 讨论(0)
  • 2021-02-10 04:29

    Something like this:

    df$up.down <- with( df, ave(count, gene,
                    FUN=function(diffs) c("up", "down")[1+(diff(diffs) < 0) ]) )
    spltdf <- split(df, df$up.down)
    
    > df
      gene condition count  sd up.down
    1    A   control    10 1.0    down
    2    A treatment     2 0.2    down
    3    B   control     5 0.1      up
    4    B treatment     8 2.0      up
    5    C   control     5 0.8    down
    6    C treatment     1 0.1    down
    > spltdf
    $down
      gene condition count  sd up.down
    1    A   control    10 1.0    down
    2    A treatment     2 0.2    down
    5    C   control     5 0.8    down
    6    C treatment     1 0.1    down
    
    $up
      gene condition count  sd up.down
    3    B   control     5 0.1      up
    4    B treatment     8 2.0      up
    
    0 讨论(0)
提交回复
热议问题