0

Edit with example**

gene    pvalue    log2FoldChange
a        1         -1.5
b        0.01      -2
c        0.004      1.4

I have a dataframe like this ^^

I want to specify a cutoff for pvalue and a range for log2FoldChange outside of which I want the values. So I want ones which have a log2FoldChange >1 or <-1 and a pvalue of <0.05 I've tried:

res <- res[res$padj < 0.05 & res$log2FoldChange > 1 & res$log2FoldChange < -1,] 
res <- res[complete.cases(res), ]

But this didn't work! Gave me an empty dataframe called res.

ainebrowne
  • 17
  • 4
  • 1
    Would be easier to help you if you had an reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – s_baldur Jul 20 '17 at 16:23
  • Looks like the first one is missing a comma, should at least be `res <- res[res$padj < 0.05 & res$log2FoldChange > 1 & res$log2FoldChange < -1,]` – Matt Tyers Jul 20 '17 at 16:31

2 Answers2

1

res$log2FoldChange > 1 & res$log2FoldChange < -1 is impossible. Hence, the empty set is returned. If you want a p-value cut-off and values outside a range of the other variable maybe what you really mean is the following.

inx1 <- res$padj < 0.05 & res$log2FoldChange > 1
inx2 <- res$padj < 0.05 & res$log2FoldChange < -1

res <- res[inx1 | inx2, ]
res <- res[complete.cases(res), ]

Obviously, this is completeky untested, since you've posted no data.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0

You may try this (I assume you want log2FoldChange to lie in the interval R - (-1,1)

res[which(res$padj < 0.05 & (res$log2FoldChange > 1 || res$log2FoldChange< -1)),]
Raj Padmanabhan
  • 540
  • 5
  • 11