I am aware that a for-loop isn't the best thing to use in R. Please suggest an alternative to the snippet below:
mat
is a 2-D matrix
pos.scores
and neg.scores
are 1-D matrices
I am trying to populate the pos.scores
and neg.scores
matrices using some computation (if-else statements).
for (i in 1:(ncol(mat)-1))
{
pos.scores[1,i]<-ifelse(sum(mat[,i]>0 & mat[,ncol(mat)]=="Positive")==0,0.000001,sum(mat[,i]>0 & mat[,ncol(mat)]=="Positive"))/nrow(mat)
neg.scores[1,i]<-ifelse(sum(mat[,i]>0 & mat[,ncol(mat)]=="Negative")==0,0.000001,sum(mat[,i]>0 & mat[,ncol(mat)]=="Negative"))/nrow(mat)
}