I am using this code but still seeing that the negative values of the gene are upregulating.
DEGdf <- mutate(data, sig=ifelse(data$pvalue<=0.05 & data$log2FoldChange>=2 | data$pvalue>0.05 & data$log2FoldChange<2, "up", "down"))
Please see the attached image. Any help would be much appreciated.
It may be better to wrap within ()
for each block of logical expressions and we don't need the data$
within tidyverse
functions (it may work in ungrouped data, but if the data is grouped, then it takes the whole column instead of the values of the columns within the group). In the compound logical expression, the second expression with log2FoldChange < 2
may be log2FoldChange < -2
The reason is that
with(data, (pvalue > 0.05 & log2FoldChange < 2))
#[1] FALSE FALSE TRUE TRUE
returns TRUE for those 2 cases and thus in the ifelse
, the 'yes' option i.e. 'up' got selected. If we change that to -2
, it may work
library(dplyr)
data %>%
mutate(sig = ifelse((pvalue <= 0.05 & log2FoldChange >= 2)|
(pvalue > 0.05 & log2FoldChange < -2), "up", "down"))
-output
# log2FoldChange pvalue sig
#1 -3.55780 0.000081500 down
#2 -3.40030 0.002576522 down
#3 -0.76560 0.116639300 down
#4 -1.01844 0.131739900 down
data <- structure(list(log2FoldChange = c(-3.5578, -3.4003, -0.7656,
-1.01844), pvalue = c(8.15e-05, 0.002576522, 0.1166393, 0.1317399
)), class = "data.frame", row.names = c(NA, -4L))