[R] add a special column to a matrix

Jim Lemon drjimlemon at gmail.com
Wed Jul 8 12:32:16 CEST 2015


Hi Lida,
I think what you want is:

mat1<-read.table(text="a    b    c     d
fg    1:23       dfgv       5
pt    10:18     tgtgh      1
wq   15:123    oiljk       6
fg     9:1323    ass       4
yr     12:123    kjjlk       5",header=TRUE)

mat2<-read.table(text="b     q
 1:23      0
 10:18     1
 14:455    2
 15:123    2
 9:1323    2
 12:123    1
 5:1548    0",header=TRUE)

merge(mat1,mat2,by="b")

You don't seem to have a column name for the first column in mat2, so
I have added one. If it is different from mat1, you will have to use a
slightly different "b" argument.

Jim

On Wed, Jul 8, 2015 at 4:06 PM, PIKAL Petr <petr.pikal at precheza.cz> wrote:
> Hi
>
> See answers in line
>
> From: Lida Zeighami [mailto:lid.zigh at gmail.com]
> Sent: Tuesday, July 07, 2015 10:20 PM
> To: PIKAL Petr
> Subject: Re: [R] add a special column to a matrix
>
>
> Hi Petr,
>
> Thanks, I can solve it!
> On Jul 7, 2015 11:05 AM, "Lida Zeighami" <lid.zigh at gmail.com<mailto:lid.zigh at gmail.com>> wrote:
> Hi Petr,
>
> Thank you so much for replying!
> I have two problems:
>
>
> 1.      I couldn't understand what you mean "No HTML posting ", would you please explain more about that?
>
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
> 2. still my problem hasn't been solved! I don't want to add the columns and rows of two matrices together!
> just I want  to add the q column from mat2 to mat1  ( column b in mat1 and the rownames in mat1 are the same but not in a same order! )
>
> I should keep the mat1 and just add the value of rownames of mat2 to the equivalent elements in column b( I should add column q from mat2 to mat1 regarding the column b)
>
>
> Did you try read help page of merge command?
>
> Did you even try merge? If yes please describe what you do not like about it.
>
> merge(mat1, mat2, all.x=TRUE)
>
> shall take all lines of mat1 and add corresponding columns of mat2 to appropriate rows.
>
> However we do not know anything about your data. From your posting it seems to me that
>
> dim(mat1) results in c(n,4) and dim(mat2) results in (n,1) and what do you consider as a column in mat2 are row names. In that case you need to add a column of row names, maybe
>
> mat2$b <- rownames(mat2)
>
> before calling merge
>
> Cheers
> Petr
>
>
>
> head(mat1)
> a       b         c           d
> fg    1:23       dfgv       5
> pt    10:18     tgtgh      1
> wq   15:123    oiljk       6
> fg     9:1323    ass       4
> yr     12:123    kjjlk       5
>
> my second matrix:
> head(mat2)
>                      q
>   1:23             0
>    10:18           1
>   14:455          2
>    15:123         2
>     9:1323        2
>     12:123        1
>      5:1548        0
>
> output should be :
> head(mat)
> a       b         c           d         q
> fg    1:23       dfgv       5        0
> pt    10:18     tgtgh      1        1
> wq   15:123    oiljk       6        2
> fg     9:1323    ass       4        2
> yr     12:123    kjjlk       5        1
>
> Thanks again,
> Lida
>
> On Tue, Jul 7, 2015 at 9:53 AM, PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>> wrote:
> Hi
>
> No HTML posting please, sometimes the mail is unreadable
>
> It is a work for merge.
>
> mat3 <- merge(mat1, mat2, all=TRUE)
>
> shall do the trick (untested)
>
> 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 Lida
>> Zeighami
>> Sent: Tuesday, July 07, 2015 4:43 PM
>> To: r-help at r-project.org<mailto:r-help at r-project.org>
>> Subject: [R] add a special column to a matrix
>>
>> Hi there,
>>
>> I have a two matrices which they have a common column! I want to add
>> the a column of second to matrix to the equivalent column of first
>> matrix!
>> my first matrix is
>> head(mat1)
>> a       b         c           d
>> fg    1:23       dfgv       5
>> pt    10:18     tgtgh      1
>> wq   15:123    oiljk       6
>> fg     9:1323    ass       4
>> yr     12:123    kjjlk       5
>>
>> my second matrix:
>> head(mat2)
>>        e            q
>>   1:23             0
>>    10:18           1
>>   14:455          2
>>    15:123         2
>>     9:1323        2
>>     12:123        1
>>      5:1548        0
>>
>> mat1 and mat2 have a common column (b and e) but is not in the same
>> order, I want to add the q column from mat2 to be added to mat1 my
>> output will be:
>>
>> head(mat)
>> a       b         c           d         q
>> fg    1:23       dfgv       5        0
>> pt    10:18     tgtgh      1        1
>> wq   15:123    oiljk       6        2
>> fg     9:1323    ass       4        2
>> yr     12:123    kjjlk       5        1
>>
>> would you please let me know how to do it?
>>
>> Thanks
>>
>>       [[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.
>
>
> ________________________________
> 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]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list