[R] Adding two files into one and vlookup

Petr PIKAL petr.pikal at precheza.cz
Thu Jul 8 11:19:02 CEST 2010


Hi

see

?merge

Regards
Petr



r-help-bounces at r-project.org napsal dne 07.07.2010 17:07:34:

> Say I have two files file and file2:
> 
> file1 contains the following:
> Date                   Price
> 02/07/2010   53.96597903
> 03/07/2010   56.92825807
> 04/07/2010   39.27408645
> 05/07/2010   42.59834151
> 06/07/2010   70.68512383
> 07/07/2010   10.92505265
> 08/07/2010   52.12492249
> 09/07/2010   49.88767957
> 
> file2 contains the following:
> Date                   Price
> 03/07/2010   5.312006403
> 04/07/2010   673.0705924
> 05/07/2010   442.4679386
> 06/07/2010   851.9158985
> 07/07/2010   581.8592424
> 
> I want to create a new file that should look like:
> Date                  Price1                Price2
> 03/07/2010   5.312006403        56.928
> 04/07/2010   673.0705924        39.274
> 05/07/2010   442.4679386        42.598
> 06/07/2010   851.9158985        70.685
> 07/07/2010   581.8592424        10.925
> 
> Thx
> 
> 
> 
> On 7/7/10, Erik Iverson <eriki at ccbr.umn.edu> wrote:
> > raghu wrote:
> >> I have two files with dates and prices in each. The number of rows in 
each
> >> of
> >> them will differ. How do I create a new file which contains data from 
both
> >> these files? Cbind and merge are not helpful. For cbind because the 
rows
> >> are
> >> not the same replication occurs. Also if I have similar data how do I
> >> write
> >> a vlookup kind of function? I am giving an example below:
> >> Say Price1 file contains the following:
> >> Date             Price
> >> 2/3/2010       134.00
> >> 3/3/2010       133.90
> >> 4/3/2010       135.55
> >>
> >> And say price2 contains the following:
> >> Date              Price
> >> 2/3/2010        2300
> >> 3/3/2010        3200
> >> 4/3/2010        1800
> >> 5/3/2010        1900
> >>
> >> I want to take both these data together in a single file, and take 
the
> >> smaller vector (or matrix or dataframe??..i am new to R and still 
confused
> >> with the various objects) which is file1 (because it contains fewer 
rows )
> >> and vlookup prices in the second file basedon the dates on file1 and 
write
> >> three columns (date, price from 1 and price from2) in a new file. How 
do i
> >> do this please?
> >
> > I think all this can be accomplished with merge.  Can you give 
reproducible
> > examples as the posting guide suggests?
> >
> > Use read.table to read in your data into R objects, then use ?dput to 
give
> > us
> > the exact copies of the objects (probably data.frames by your 
example), and
> > what
> > output you want to have.  Being precise with the classes of objects 
you're
> > working with is key, and ?dput is a great way to make sure we have the 
same
> > objects as you.
> >
> > Another tip is common terminology. For instance, `vlookup` is not a 
term
> > used in
> > R, and many people will not know what it means.
> >
> > This way, everything is reproducible for us, and we can offer 
suggestions
> > and
> > show you what the exact output will be.  In short, making sure 
everyone is
> > on
> > the same page goes a long way when getting help from a mailing list.
> >
> >
> 
> 
> -- 
> 'Raghu'
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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