Re: [R] Merging lists on common key (was <no subject>)

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Thu 15 Dec 2005 - 14:19:02 EST

On Wed, 2005-12-14 at 18:14 -0800, Marco Blanchette wrote:
> Dear all,
>
> I am still fairly new to R and try to analyze large tables of data generated
> from genomic experiment. Currently, I am trying to plot pair of experiments
> coming from different file, trying to look at the behavior of individual
> feature in pair of experiment.
>
> My problem is that I have independent list from different source and I would
> like to plot the pair of value using a common key. As in this simplified
> version:
>
> table1 = list(CGID=c("CG_1","CG_3","CG_2", "CG_4", "CG_5"),
> diff=c(3,5,6,4,3))
>
> table2 = list(CGID=c("CG_2","CG_3","CG_4", "CG_1", "CG_5"),
> diff=c(4,6,3,9,10))
>
> How can link the two table trough the CGIDC column and plot the data from
> the 2 tables.
>
> Many tx
>
> Marco Blanchette, Ph.D.

Marco,

Please use an informative subject when posting. It makes it easier for folks, especially when reviewing the e-mail list archives.

There is a function called merge() which will perform SQL-like joins.

merge() will coerce the two lists to data frames, so you can do the following:

> merge(table1, table2, by = "CGID")

  CGID diff.x diff.y

1 CG_1      3      9
2 CG_2      6      4
3 CG_3      5      6
4 CG_4      4      3
5 CG_5      3     10

The result is a join of the two lists, using CGID (quoted) as the primary key. The two 'diff' elements are uniquely named based upon their source objects (x and y arguments in merge()) as a suffix. The appended suffix can be changed if required.

See ?merge for more information.

HTH, Marc Schwartz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Dec 15 14:27:32 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:39 EST