I have a matrix 1014*100. I only need rows with p.value< 0.05 so I've tried to filter my matrix in different ways.
TCGA.A7.A0CE.01A.11R.A00Z.07 TCGA.A7.A0CE.11A.21R.A089.07
hsa04014 0.0010059007 0.0009569162
hsa04010 0.0007198587 0.0006481671
hsa04015 0.0008544083 0.0006697753
First way:
threshold<-0.05
matrix2<-my_matrix[apply(my_matrix,1,min)<threshold,]
The output is a matrix 463*100 but some rows have a p.value > 0.05.
Second way:
matrix2 <-NULL
for(i in 1:1014){
n<- my_matrix[[i]]
for(i in 1:100){
if(n[[i]] < 0.05){
n[[i]]<-i
}
else{
n[[i]] <- NA
} }
matrix2<- rbind(matrix2, n)
}
There is something wrong, I think in the if. Is there anyone who can help me? Thanks in advance.
Your solution worked fine for me, but maybe I didnt create the data.frame correctly. You could do it in 1 step, with your method, or in 2 steps, where you create a column, where the minimum value is stored, and then filter according to that column.
my_matrix <- data.frame(
TCGA.A7.A0CE.01A.11R.A00Z.07 = c(0.051, 0.09, 0.04),
TCGA.A7.A0CE.11A.21R.A089.07 = c(0.06, 0.01 , 0.067)
)
row.names(my_matrix) <- c("hsa04014","hsa04010","hsa04015")
threshold<-0.05
## In 1 step (your solution):
my_matrix[(apply(my_matrix,1,min)) < threshold,]
## In 2 steps:
my_matrix$min <- as.numeric(apply(my_matrix,1,min))
my_matrix[my_matrix$min < threshold,]