Need help to remove NA values from data frame
2
0
Entering edit mode
6 weeks ago

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 • 755 views
ADD COMMENT
1
Entering edit mode
6 weeks ago

Hi Anas,

If your data frame is called res, then:

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

Kevin

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
colnames(res)[1] <- 'gene names'

...or:

idx <- which(colnames(res) == 'X')
idx
colnames(res)[idx] <- 'gene names'
ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
6 weeks ago
rioualen ▴ 580

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)
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

whatever, %>% need this. :-D

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 1944 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6