[R] For help in R coding

Bansal, Vikas vikas.bansal at kcl.ac.uk
Tue Jul 5 18:16:59 CEST 2011

```Yes.this is perfect.but can i use the and (&) operator to check if two column have same value.Like-

merge(frame1,frame2,by="x&G")
according to your data given in previous mail.

so the output should be-

x   G   A.x C.x  A.y C.y
4  -1   1    1       -1    0
8   -1  0    0       -1   -1

Thanking you,
Warm Regards
Vikas Bansal
Msc Bioinformatics
Kings College London
________________________________________
Sent: Tuesday, July 05, 2011 4:49 PM
To: Bansal, Vikas
Cc: r-help at r-project.org
Subject: Re: [R] For help in R coding

If I understand correctly, you want to keep the rows from each table
which have common values in the second column.  In which case, merge
will work for this, such as in this example.

Say you have these data frames:

> frame1
x  A  C  G
1  0 -1  2
2 -1  0 -1
3  0  0 -1
4  1  1 -1
5  0  1  0
6 -1  1 -1
7  0 -1  1
8  0  0 -1
9  1 -3  0
10  0  0  0

> frame2
x  A  C  G
2  0  1  0
4 -1  0 -1
6  1  0  0
8 -1 -1 -1
10 -1  0 -1
12  1 -1  0
14  0 -2  0
16  0 -2  0
18  1  0 -1
20  0 -1  2

and you want to combine these tables and keep the rows which have
values of column x in common or getting something like this.

x   A  C  G   A  C  G
2  -1   0  -1   0   1   0
4   1   1  -1  -1   0  -1
6  -1   1  -1   1   0   0
8   0   0  -1  -1  -1  -1
10   0   0   0  -1   0  -1

which shows that the common values of x between the two tables is
2,4,6,8, and 10.  The merge command to do this is:

> merge(frame1,frame2,by="x")
x  A.x C.x G.x A.y C.y G.y
2   -1    0    -1    0    1    0
4    1    1    -1   -1    0   -1
6   -1    1    -1    1    0    0
8    0    0    -1   -1   -1   -1
10   0    0     0    -1   0   -1

which is the same as the desired output from before except for the
column names.  When there are common names between the two data frames
being merged, R will add an extension to the column name to make it
easy to determine which columns came from which data frame.  In this
case, the .x extension are columns from the first data frame (frame1
in this example) and the .y extension are columns from the second data
frame (frame 2 in this example).

I hope this helps!

On Tue, Jul 5, 2011 at 11:02 AM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
>
> Yes sir.I have already looked at merge()
> but as I am new to R,I was not able to understand the argument that how should i create a code for the logic i gave in previous mail .
>
>
> Thanking you,
> Warm Regards
> Vikas Bansal
> Msc Bioinformatics
> Kings College London
> ________________________________________
> From: ONKELINX, Thierry [Thierry.ONKELINX at inbo.be]
> Sent: Tuesday, July 05, 2011 3:53 PM
> To: Bansal, Vikas; David Winsemius
> Cc: r-help at r-project.org
> Subject: RE: [R] For help in R coding
>
> Dear Vikas,
>
> Have at look at ?merge()
>
> Best regards,
>
> Thierry
>
>> -----Oorspronkelijk bericht-----
>> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> Namens Bansal, Vikas
>> Verzonden: dinsdag 5 juli 2011 16:51
>> Aan: David Winsemius
>> CC: r-help at r-project.org
>> Onderwerp: Re: [R] For help in R coding
>>
>> Dear all,
>>
>> I have one problem and did not find any solution.Please I want your help.
>>
>> I have two data frames and I want to concatenate them.But the thing is-
>>
>> two data frames are like this-
>>
>> V1           V2               A       C       G       T
>> 10    135344109       0       0       1       0
>> 10    135344110       0       1       0       0
>> 10    135344111       0       0       1       0
>> 10    135344112       0       0       1       0
>> 10    135344113       0       0       1       0
>> 10    135344114       1       0       0       0
>> 10    135344115       1       0       0       0
>> 10    135344116       0       0       0       1
>> 10    135344117       0       1       0       0
>> 10    135344118       0       0       0       1
>>
>> second data frame-
>>
>> V1           V2               A       C       G       T
>> 10    135344111       1       0       1       0
>> 10    135344113       0       0       1       0
>> 10    135344109       0       3       1       0
>> 10    135344114       1       0       0       0
>> 10    145344115       1       0       0       0
>> 10    135344116       1       0       0       1
>> 10    132344117       0       1       0       0
>> 10    135344118       0       0       0       1
>> 10    135344110       0       1       0       0
>>
>> now i have to create a new data frame which has  insert column 3,4,5 and 6 of
>> second data frame in first data frame if the value in second column is same in
>> both the data frames (values in V2 column).So the output(new data frame)
>> should be-
>>
>> V1           V2               A       C       G       T     A       C     G      T
>> 10    135344109       0       0       1       0      0        3       1       0
>> 10    135344110       0       1       0       0      0        1       0       0
>> 10    135344111       0       0       1       0      1        0       1       0
>> 10    135344113       0       0       1       0      0        0       1       0
>> 10    135344114       1       0       0       0      1        0       0       0
>> 10    135344116       0       0       0       1      1        0       0       1
>> 10    135344118       0       0       0       1      0        0       0       1
>>
>> I f you see the output, second column values-
>>
>>              V2
>>       135344109
>>       135344110
>>       135344111
>>       135344113
>>       135344114
>>       135344116
>>       135344118
>>
>> these values are common in both input dataframes.
>>
>>
>>
>>
>>
>>
>> Thanking you,
>> Warm Regards
>> Vikas Bansal
>> Msc Bioinformatics
>> Kings College London
>> ________________________________________
>> From: David Winsemius [dwinsemius at comcast.net]
>> Sent: Monday, July 04, 2011 12:11 AM
>> To: Bansal, Vikas
>> Cc: Dennis Murphy; r-help at r-project.org
>> Subject: Re: [R] For help in R coding
>>
>> On Jul 3, 2011, at 6:10 PM, Bansal, Vikas wrote:
>>
>> >
>> > ________________________________________
>> > From: David Winsemius [dwinsemius at comcast.net]
>> > Sent: Sunday, July 03, 2011 7:08 PM
>>
>> >>
>> >
>> > the code is same i just want to add a condition so that  it should
>> > check that if in column 3, the character is A then make number of A
>> > equal to total number of . and ,
>> >
>> > Should I explain better or can you please tell me which thing is not
>> > clear?
>>
>> My second posting today had a solution.
>>
>>
>> >
>> >>
>> > --
>> > David.
>> >>
>> >>
>> >>
>> >> we can also do it by using some other condition rather than if
>> >> condition?
>> >>
>> >
>>
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--