arrays - how to find particular rows or columns in R with specific conditions -


for example, if have matrix or array following format

m = matrix(c(1,1,0,0,0,1,0,1,0,0,0,0,1,1,0,1), # data elements      nrow=4,              # number of rows      ncol=4,              # number of columns      byrow = true)        # fill matrix rows  

how can find index of rows , columns have 0 inside? if have more complicated matrix in need find out in rows or columns numbers inside in specific interval?

m = matrix(c(1,1,12,34,0,19,15,1,0,17,12,0,21,1,11,1), # data elements      nrow=4,              # number of rows      ncol=4,              # number of columns      byrow = true)        # fill matrix rows  

how should find column in numbers between (10 - 20)?

thank 1 can on this.

and, also, can not use or while loops deal it.

you can that:

 which(apply(m, 1, sum) == 0 ) [1] 3  which(apply(m, 2, sum) == 0 ) [1] 3 

where 1 search rows , 2 columns. results tell row , column has zeros. m can see row number 3 , column number 3 have zeros.

or suggested akrun can use rowsums , colsums should faster.

 which(rowsums(m) == 0) [1] 3  which(colsums(m) == 0) [1] 3 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -