This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Need help to remove NA values from data frame

I have this data frame : df

and I want to remove those rows which contain NA values from the log2fold change column

How can I do this through R?

deseq2 r

2 answers

Hi Anas,

If your data frame is called res, then:

res[!is.na(res$log2FoldChange),]

Kevin

I also want to rename column 'X' to 'gene names' . How can i do it?

colnames(res)[1] <- 'gene names'

...or:

idx <- which(colnames(res) == 'X')
idx
colnames(res)[idx] <- 'gene names'

I am trying this to make a heatmap:

ntd <- normTransform(dds)
library("pheatmap")
select <- order(rowMeans(counts(dds,normalized=TRUE)),
                decreasing=TRUE)[1:50]
df <- as.data.frame(colData(dds)[,c("group")])
pheatmap(assay(ntd)[select,], cluster_rows=FALSE, show_rownames=FALSE,
         cluster_cols=FALSE, annotation_col=df)

But it is giving me this error:

Error in check.length("fill"): 'gpar' element 'fill' must not be length 0

The data frame which I am using for annotation is: data

kindly help me

Hi,

For dataframe manipulation you should look into the dplyr and tidyr libraries. You can take a look a this cheatsheet for example.

You can remove NAs and rename columns as follows:

library(dplyr)

your_dataframe %>%
  dplyr::filter(!is.na(log2FoldChange)) %>%
  dplyr::rename(gene_names = X)

No need to load dplyr if you call it's functions directly though. Ü

It helps avoiding confusion, since these functions also exist in other libraries

ponganta means you don't need the library(dplyr) step if you call functions like this dplyr::filter()...

whatever, %>% need this. :-D

Agreed, especially regarding dplyr::rename(). I didn't think about that.

Regarding these two functions, there are oftentimes namespace collisions with other packages, I guess this is what @rioualen meant. Good catch.

Log in to answer this question.