Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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"),
condition=c("control","treatment","control","treatment","control","treatment"),
count=c(10, 2, 5, 8, 5, 1), 
sd=c(1, 0.2, 0.1, 2, 0.8, 0.1))

  gene condition count  sd
1    A   control    10 1.0
2    A treatment     2 0.2
3    B   control     5 0.1
4    B treatment     8 2.0
5    C   control     5 0.8
6    C treatment     1 0.1

I want to calculate if there is an increase or decrease in "count" after treatment and mark them as such and/or subset them. That is (pseudo code):

for each unique(gene) do 
   if df[geneRow1,3]-df[geneRow2,3] > 0 then gene is "up"
       else gene is "down"

This what it should look like in the end (the last columns is optional):

up-regulated
 gene condition count  sd  regulation
 B    control     5    0.1    up
 B    treatment   8    2.0    up

down-regulated
 gene condition count  sd  regulation
 A    control     10   1.0    down
 A    treatment   2    0.2    down
 C    control     5    0.8    down
 C    treatment   1    0.1    down

I have been raking my brain with this, including playing with ddply, and I've failed to find a solution - please a hapless biologist.

Cheers.

like image 214
fridaymeetssunday Avatar asked Sep 21 '12 23:09

fridaymeetssunday


People also ask

How do you find the difference between consecutive rows in R?

diff() method in base R is used to find the difference among all the pairs of consecutive rows in the R dataframe. It returns a vector with the length equivalent to the length of the input column – 1.

How do you calculate mean across rows in R?

Calculate the Mean of each Row of an Object in R Programming – rowMeans() Function. rowMeans() function in R Language is used to find out the mean of each row of a data frame, matrix, or array.

What is diff function in R?

diff() function in R Language is used to find the difference between each consecutive pair of elements of a vector. Syntax: diff(x, lag, differences)


1 Answers

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
>     
like image 128
Justin Avatar answered Sep 27 '22 19:09

Justin