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]]
Easy enough (note that your column names are problematic, though)> mydata <- structure(list(X125 = c(0L, 1L, 2L, 0L, 2L), X255 = c(1L, 1L,+ 1L, 1L, 0L), X558 = c(0L, 0L, 2L, 0L, 0L), X2366 = c(NA, NA, + 1L, NA, 0L), X177 = c(0L, 0L, 0L, 0L, 0L), X255.1 = c(0L, 1L, + 0L, 0L, 0L)), .Names = c("X125", "X255", "X558", "X2366", "X177", + "X255.1"), class = "data.frame", row.names = c("aa", "bb", "cs", + "de", "gh"))> mydata$idx <- apply(mydata, 1, function(x)as.numeric(any(x == 2 & !is.na(x))))> mydataX125 X255 X558 X2366 X177 X255.1 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 0 0 0 1 Sarah On Mon, Aug 10, 2015 at 4:11 PM, Lida Zeighami <lid.zigh at gmail.com> wrote:> 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. >-- Sarah Goslee http://www.functionaldiversity.org [[alternative HTML version deleted]]
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.zigh at 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 at 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]]
Hi here is another approach.> cbind(mydata, idx=(rowSums(mydata==2, na.rm=T)>0)*1)X125 X255 X558 X2366 X177 X255.1 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 0 0 0 1 It shall be faster if this is an issue. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Thierry > Onkelinx > Sent: Monday, August 10, 2015 10:29 PM > To: Lida Zeighami > Cc: r-help at r-project.org > Subject: Re: [R] add an idx column to the matrix > > 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.zigh at 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 at 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 at 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
I applied this code in a loop function but since in some matrices there isn't any 2, so I got the below error: idx<- apply(lofGT_met,1, function(x)as.numeric(any(x==2 & !is.na(x)))) Error in apply(lofGT_met, 1, function(x){(any(x == 2)}) : dim(X) must have a positive length would you please let me know how to correct it? Thanks On Mon, Aug 10, 2015 at 3:27 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:> Easy enough (note that your column names are problematic, though) > > > mydata <- structure(list(X125 = c(0L, 1L, 2L, 0L, 2L), X255 = c(1L, 1L, > + 1L, 1L, 0L), X558 = c(0L, 0L, 2L, 0L, 0L), X2366 = c(NA, NA, > + 1L, NA, 0L), X177 = c(0L, 0L, 0L, 0L, 0L), X255.1 = c(0L, 1L, > + 0L, 0L, 0L)), .Names = c("X125", "X255", "X558", "X2366", "X177", > + "X255.1"), class = "data.frame", row.names = c("aa", "bb", "cs", > + "de", "gh")) > > mydata$idx <- apply(mydata, 1, function(x)as.numeric(any(x == 2 & !is.na > (x)))) > > mydata > X125 X255 X558 X2366 X177 X255.1 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 0 0 0 1 > > Sarah > > On Mon, Aug 10, 2015 at 4:11 PM, Lida Zeighami <lid.zigh at gmail.com> wrote: > >> 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. >> > -- > Sarah Goslee > http://www.functionaldiversity.org >[[alternative HTML version deleted]]
Hi the error is not caused by missing 2> set.seed(333) > x<-sample(0:1, 12, rep=T) > dim(x)<-c(3,4) > x[2,2]<-NA > cbind(x, idx=(rowSums(x==2, na.rm=TRUE)>0)*1)idx [1,] 0 1 1 0 0 [2,] 0 NA 0 0 0 [3,] 1 1 0 0 0 As Sarah pointed out, you are the only one observing such error. Without more information ? at least structure of your data see ?str but preferably part of your data causing error and a code you hardly get any sensible advice. Cheers Petr From: Lida Zeighami [mailto:lid.zigh at gmail.com] Sent: Wednesday, August 12, 2015 6:23 PM To: PIKAL Petr Subject: Re: [R] add an idx column to the matrix Dear Petr, I use your code in a loop function, but in some matrix there isn't any 2 so I got the below error: Error in rowSums(lofGT_met == 2, na.rm = T) : 'x' must be an array of at least two dimensions would you please let me know how to correct it? Thanks, Lida On Tue, Aug 11, 2015 at 1:40 AM, PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>> wrote: Hi here is another approach.> cbind(mydata, idx=(rowSums(mydata==2, na.rm=T)>0)*1)X125 X255 X558 X2366 X177 X255.1 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 0 0 0 1 It shall be faster if this is an issue. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org<mailto:r-help-bounces at r-project.org>] On Behalf Of Thierry > Onkelinx > Sent: Monday, August 10, 2015 10:29 PM > To: Lida Zeighami > Cc: r-help at r-project.org<mailto:r-help at r-project.org> > Subject: Re: [R] add an idx column to the matrix > > 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<http://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.zigh at gmail.com<mailto:lid.zigh at 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 at r-project.org<mailto:R-help at 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 at r-project.org<mailto:R-help at 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient. [[alternative HTML version deleted]]
Hi dear all, Yes, your solution all are correct! it was my mistake and I found it. Thanks On Thu, Aug 13, 2015 at 1:07 AM, PIKAL Petr <petr.pikal at precheza.cz> wrote:> Hi > > > > the error is not caused by missing 2 > > > > > set.seed(333) > > > x<-sample(0:1, 12, rep=T) > > > dim(x)<-c(3,4) > > > x[2,2]<-NA > > > cbind(x, idx=(rowSums(x==2, na.rm=TRUE)>0)*1) > > idx > > [1,] 0 1 1 0 0 > > [2,] 0 NA 0 0 0 > > [3,] 1 1 0 0 0 > > > > As Sarah pointed out, you are the only one observing such error. Without > more information ? at least structure of your data > > > > see ?str > > > > but preferably part of your data causing error and a code you hardly get > any sensible advice. > > > > Cheers > > Petr > > *From:* Lida Zeighami [mailto:lid.zigh at gmail.com] > *Sent:* Wednesday, August 12, 2015 6:23 PM > *To:* PIKAL Petr > > *Subject:* Re: [R] add an idx column to the matrix > > > > Dear Petr, > > > > I use your code in a loop function, but in some matrix there isn't any 2 > so I got the below error: > > > > Error in rowSums(lofGT_met == 2, na.rm = T) : > 'x' must be an array of at least two dimensions > > > > would you please let me know how to correct it? > > Thanks, > > Lida > > > > On Tue, Aug 11, 2015 at 1:40 AM, PIKAL Petr <petr.pikal at precheza.cz> > wrote: > > Hi > > here is another approach. > > > cbind(mydata, idx=(rowSums(mydata==2, na.rm=T)>0)*1) > X125 X255 X558 X2366 X177 X255.1 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 0 0 0 1 > > It shall be faster if this is an issue. > > Cheers > Petr > > > > > -----Original Message----- > > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Thierry > > Onkelinx > > Sent: Monday, August 10, 2015 10:29 PM > > To: Lida Zeighami > > Cc: r-help at r-project.org > > Subject: Re: [R] add an idx column to the matrix > > > > 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.zigh at 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 at 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 at 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. > > > > ------------------------------ > Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou > ur?eny pouze jeho adres?t?m. > Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? > neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie > vyma?te ze sv?ho syst?mu. > Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email > jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. > Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi > ?i zpo?d?n?m p?enosu e-mailu. > > V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: > - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? > smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. > - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; > Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany > p??jemce s dodatkem ?i odchylkou. > - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve > v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. > - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za > spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n > nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto > emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich > existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. > > This e-mail and any documents attached to it may be confidential and are > intended only for its intended recipients. > If you received this e-mail by mistake, please immediately inform its > sender. Delete the contents of this e-mail with all attachments and its > copies from your system. > If you are not the intended recipient of this e-mail, you are not > authorized to use, disseminate, copy or disclose this e-mail in any manner. > The sender of this e-mail shall not be liable for any possible damage > caused by modifications of the e-mail or by delay with transfer of the > email. > > In case that this e-mail forms part of business dealings: > - the sender reserves the right to end negotiations about entering into a > contract in any time, for any reason, and without stating any reasoning. > - if the e-mail contains an offer, the recipient is entitled to > immediately accept such offer; The sender of this e-mail (offer) excludes > any acceptance of the offer on the part of the recipient containing any > amendment or variation. > - the sender insists on that the respective contract is concluded only > upon an express mutual agreement on all its aspects. > - the sender of this e-mail informs that he/she is not authorized to enter > into any contracts on behalf of the company except for cases in which > he/she is expressly authorized to do so in writing, and such authorization > or power of attorney is submitted to the recipient or the person > represented by the recipient, or the existence of such authorization is > known to the recipient of the person represented by the recipient. >[[alternative HTML version deleted]]