[R] merging two dataframes

Petr PIKAL petr.pikal at precheza.cz
Tue Oct 25 06:57:55 CEST 2011


> 
> Thank you Petr!
> 
> I have read on the merge help page, but I cant figure out how to write 
this
> function.
> When I use your function it includes all data from "data3", but all 
columns
> in "data" has "NA"(without "name" and "date". I hoped to keep these 
values

Because name and date common columns and you do not need them twice. Merge 
shall work as you expected provided input data are OK. The result of 
command I suggested shall be all values from both data frames aligned by 
name and date.

It is impossible to say what is wrong without reproducible data. Did you 
check size of your data frames after merge? I suspect that date in data 
and date in data3 is somewhat different. You shall check it with ?str 
function.

As Posting guide says you shall provide some suitable data so that we can 
look at it and work with it. Something like 

dput(data[1:20,])

could be enough.

Regards
Petr


> to.
> 
> I try to explain it more precise:
> In "data" with 14000 observations about company name, date, size of 
dividend
> etc.
> In "data3" there are 800000 daily observations of the stockprices for 
each
> company listed:
> 
> name            date               div
> statoil          17.05.2000        5
> statoil          18.05.2001        6
> ........           ..............       ...
> Yara             17.05.2000       10
> etc
> 
> I want to get the stockprice for statoil, yara etc from "data3", and 
merge
> it into a new data like this:
> 
> name            date               div     price
> statoil          17.05.2000        5       120
> statoil          18.05.2001        6       130
> ........           ..............       ...
> Yara             17.05.2000       10      200
> etc
> 
> And also keep the rest of the columns from both datasets. 
> 
> name            date               div     price        industry secid 
> etc
> statoil          17.05.2000        5       120         ....... ..... 
> ...
> statoil          18.05.2001        6       130         ....... 
> ......   .... 
> ........           ..............       ...
> Yara             17.05.2000       10      200        ..... ...... 
> ...
> etc
> 
> Using the first function i get this result:
> 
> name            date               div     price        industry secid 
> etc
> statoil          17.05.2000        NA      120         ....... ..... 
> ...
> statoil          18.05.2001        NA      130         ....... 
> ......   .... 
> ........           ..............       ...
> Yara             17.05.2000       NA     200        ..... ...... 
> ...
> 
> Hope you understand what i want to do.
> 
> Thanks again, I really appreciate it!
> etc
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/merging-two-
> dataframes-tp3932869p3933101.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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