Dear Lida, Here is a solution. Please don't post in HTML. And provide an easy to use example of the data. E.g. the output of dput(mydata)
set.seed(1234) mydata <- matrix( sample( c(0, 1, 2, NA), size = 30, replace = TRUE, prob = c(2, 1, 1, 1) ), ncol = 6 ) idx <- apply(mydata, 1, function(x){any(x == 2)}) idx[is.na(idx)] <- FALSE cbind(mydata, idx) ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey 2015-08-10 22:11 GMT+02:00 Lida Zeighami <lid.z...@gmail.com>: > Hi there, > > I have a matrix contain 0,1,2, NA elements. > I want to add a column to this matrix with name of "idx" . then for each > row, I should put 1 in this column (idx) if there is at least one 2 in that > row otherwise I should put 0 in this column! > > for example mydata: > > 125 255 558 2366 177 255 > aa 0 1 0 NA 0 0 > bb 1 1 0 NA 0 1 > cs 2 1 2 1 0 0 > de 0 1 0 NA 0 0 > gh 2 0 0 0 0 0 > > > my output should be: > > > 125 255 558 2366 177 255 idx > aa 0 1 0 NA 0 0 0 > bb 1 1 0 NA 0 1 0 > cs 2 1 2 1 0 0 1 > de 0 1 0 NA 0 0 0 > gh 2 0 0 2 0 2 1 > > Thank you for your help. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.