this works, thank you! strangely, when I have many conditions this does not happen, do you know why? thank you again
• 0 views
•
link
Hello, I have a gene count matrix with 1 condition only. I have made log2(raw_count+1) with DESeq2, but when I filter off genes with zero expression my gene names disappear. How to fix this so that at the end I have my gene names and log2 values?
raw_count <- read.csv("gene_count_matrix.csv", header = TRUE, row.names = 1)
# Remove all gene which has 0 value in all sample
all <- apply(raw_count, 1, function(x) all(x==0))
newdata <- raw_count[!all,]
write.csv(newdata, file = "filtered_matrix.csv")
final_count <- read.csv("filtered_matrix.csv", header=TRUE, row.names = 1)
You should use drop=FALSE to prevent droping the dimensions of the matrix.
newdata <- raw_count[!all, , drop=F]
this works, thank you! strangely, when I have many conditions this does not happen, do you know why? thank you again
Because if you have only one column in your matrix/dataframe, the default behaviour of R is to convert it into a vector. Edit : more info in the R help :
?'['
I can't reproduce your issue:
M <- matrix(c(0,0,0,1,0,1, 1, 2,3 ), nrow = 3, byrow = TRUE)
rownames(M) <- letters[1:3]
drop.rows <- apply(M, 1, function(x) all(x==0)) # best not to use the function-name `all`
M[!drop.rows, ]
> M[which(!drop.rows), ]
[,1] [,2] [,3]
b 1 0 1
c 1 2 3
Could you post a minimal example, please.
Try with only 1 column :
M <- matrix(c(0,0,0,1,0,1, 1, 2,3 ), nrow = 9, byrow = TRUE)
rownames(M) <- letters[1:9]
drop.rows <- apply(M, 1, function(x) all(x==0)) # best not to use the function-name `all`
M[!drop.rows, , drop=T]
d f g h i
1 1 1 2 3
M[!drop.rows, , drop=F]
[,1]
d 1
f 1
g 1
h 2
i 3
Log in to answer this question.