0

say you have in R

m = matrix(0, 10, 5, dimnames = list(
                                    c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), 
                                    c(1, 2, 3, 4, 5)))
m[1,] = c(0,0,0,0,1)
m[2,] = c(0,0,0,1,1)
m[3,] = c(0,0,1,1,1)
m[4,] = c(0,0,1,1,0)
m[5,] = c(1,0,0,0,0)
m[6,] = c(1,1,1,0,0)
m[7,] = c(0,1,1,0,0)
m[8,] = c(0,1,1,0,0)
m[9,] = c(0,1,1,1,0)
m[10,] = c(1,1,1,0,1)

Above you see matrix m. I have another matrix p such that:

p = matrix(0, 1, 5, dimnames = list(
                                    c("A"), 
                                    c(1, 2, 3, 4, 5)))
p[1,] = c(0.2,0.03,0.2,0.01,1)

I have a matrix with say specific values (p-values), just one column. I want to make a new matrix from m but only columns where the value for that column in p is say < 0.05

So basically, I want in R:

for (i in 1:length(p){
    if (p[1,i] < 0.05){
        #remember i in a list to extract later?
        mycols.append(i)
}}

myNewMat <- matrix[,mycols]

thx

StudentOfScience
  • 809
  • 3
  • 11
  • 35
  • Please check this [link](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). A good reproducible example will help others to tackle your question lot more easily. – CHP Nov 12 '13 at 10:05

1 Answers1

1

Try below and see if that's what you want.

myNewMat <- m[,p < 0.05]
CHP
  • 16,981
  • 4
  • 38
  • 57