2

I am trying to write a function that filters out all rows that contain NA and where the adjusted pvalue is lower than a certain threshold.

Code

filter_results <- function(result, pvalue){
    df <- as.data.frame(result)
    df<- df %>%
        filter(!is.na(padj))  %>%
        filter(padj< pvalue)
    return(df)
}

dds_res_filtered_pvalue <- filter_results(dds_res_simple, 0.01)

However, this function returns an empty dataframe. Manual input of pvalue works as intended:

filter_results <- function(result){
    df <- as.data.frame(result)
    df<- df %>%
        filter(!is.na(padj))  %>%
        filter(padj< 0.01) # manual input of pvalue
    return(df)
}

dds_res_filtered_pvalue <- filter_results(dds_res_simple)

Question

How can I pass numeric values into functions that use dplyr?

user213544
  • 2,046
  • 3
  • 22
  • 52
nhaus
  • 786
  • 3
  • 13

1 Answers1

1

Looking at the name of your data frame and column names, I guess you are using DESeq2. There is a column called pvalue in your data frame, so you have to use another variable, otherwise it uses the column pvalue in your filter():

library(DESeq2)
library(dplyr)
set.seed(100)
dds = makeExampleDESeqDataSet(betaSD = 1)
dds = DESeq(dds)
dds_res_simple = results(dds)

colnames(dds_res_simple)
[1] "baseMean"       "log2FoldChange" "lfcSE"          "stat"          
[5] "pvalue"         "padj"   

Now we modify your function, using "cutoff" instead of pvalue:

filter_results <- function(result, cutoff){
    df <- as.data.frame(result)
    df<- df %>%
        filter(!is.na(padj))  %>%
        filter(padj< cutoff)
    return(df)
}

And it works:

head(filter_results(dds_res_simple,0.05))

 baseMean log2FoldChange     lfcSE      stat       pvalue         padj
1  38.01463       1.714453 0.3723547  4.604353 4.137494e-06 6.173404e-05
2  70.35889       1.503615 0.3152131  4.770152 1.840868e-06 2.983475e-05
3  18.94027       1.558557 0.5314113  2.932864 3.358511e-03 1.635751e-02

max(filter_results(dds_res_simple,0.05)$padj)
0.04594662
StupidWolf
  • 45,075
  • 17
  • 40
  • 72