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
Post a Comment