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

前端 未结 2 1929
一整个雨季
一整个雨季 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: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
    

提交回复
热议问题