s-news
[Top] [All Lists]

Re: Adding to a data frame

To: "Kim Elmore" <Kim.Elmore@noaa.gov>, "S-News" <s-news@wubios.wustl.edu>
Subject: Re: Adding to a data frame
From: "Stewart, William" <william.stewart@rbc.com>
Date: Tue, 13 Nov 2007 13:03:16 -0500
In-reply-to: <4739DF4B.3060203@noaa.gov>
References: <4739DF4B.3060203@noaa.gov>
Thread-index: AcgmGwDynhhZSXkATCK5VzecMtrQuAAAXCiQ
Thread-topic: [S] Adding to a data frame
The merge command should do what you want.

merge(df.1, df.2, by="index") 


------------------------------------- 
William A. Stewart 
Senior Manager, Operational Risk 
RBC Financial Group 
william.stewart@rbc.com 
phone: 416-974-3019 


-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Kim Elmore
Sent: 2007, November, 13 12:31 PM
To: S-News
Subject: [S] Adding to a data frame

I have a data frame with entries something like the following:

index  value1  value2  value3
1          0.5          3.5      4
1          1.0          4.0      5
1          1.5          5.0      6
1          3.0          3.5      7
2          0.6          3.2      4
2          1.5          3.8      7
2          2.8          3.3      7
3          0.8          4.2      9
etc

I have another data frame that looks like this:

index  value4  value5  value6
1         33.4     106.9   78
2         34.5     107.0   88
3         32.8     108.2   90
etc

Keying on the "index" value, I want to merge these two data frames to
get the following:

index  value1  value2  value3  value4  value5  value6
1          0.5          3.5      4       33.4     106.9   78
1          1.0          4.0      5       33.4     106.9   78
1          1.5          5.0      6       33.4     106.9   78
1          3.0          3.5      7       33.4     106.9   78
2          0.6          3.2      4       34.5     107.0   88
2          1.5          3.8      7       34.5     107.0   88
2          2.8          3.3      7       34.5     107.0   88
3          0.8          4.2      9       32.8     108.2   90
etc.

I can do this row-by-row using a for loop but that seems terribly
inefficient, especially given that I have about 30-40,000 entries to
process. How can I do this more efficiently? I've tried obvious
approaches, but all of what I dreamed up require that the two data
frames have the same number of rows.

Kim Elmore
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news
_______________________________________________________________________

This e-mail may be privileged and/or confidential, and the sender does not 
waive any related rights and obligations.
Any distribution, use or copying of this e-mail or the information it contains 
by other than an intended recipient is unauthorized.
If you received this e-mail in error, please advise me (by return e-mail or 
otherwise) immediately.  

Ce courrier électronique est confidentiel et protégé. L'expéditeur ne renonce 
pas aux droits et obligations qui s'y rapportent.
Toute diffusion, utilisation ou copie de ce message ou des renseignements qu'il 
contient par une personne autre que le (les) destinataire(s) désigné(s) est 
interdite.
Si vous recevez ce courrier électronique par erreur, veuillez m'en aviser 
immédiatement, par retour de courrier électronique ou par un autre moyen.

<Prev in Thread] Current Thread [Next in Thread>