[R] add an idx column to the matrix
Lida Zeighami
lid.zigh at gmail.com
Wed Aug 12 13:48:03 CEST 2015
Dear all,
Thank you so much for your helps.
They all works! But I think the Petr solution is the best!
Thanks again.
On Aug 11, 2015 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]]
More information about the R-help
mailing list