r - How to find all the matches in a table? -
this question has answer here:
- find indices of non 0 elements in matrix 2 answers
i know function
match(x, dataset)
but shows first position of match found , need specify column in table in order result. if there's table this
x1 x2 ... 1 3 1 ... 2 4 2 ... 3 1 1 ... 4 2 4 ...
how can find position of number 1s? want row , column number separately. result of above example should 1,3(row) , x1,x2(column).
dat == 1
give matrix positions equal 1 true
otherwise false
.
additionally, can find row
, column
positions of elements meet condition (in case, equal 1 istrue
), using which
, arr.ind
argument.
your data
dat <- read.table(header=true, text= "x1 x2 1 3 1 2 4 2 3 1 1 4 2 4 ")
extract positions data equals one
which(dat==1, arr.ind=true) # row col # 3 3 1 # 1 1 2 # 3 3 2
Comments
Post a Comment