[R] I really need help to merge two data frames

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Nov 2 09:50:31 CET 2011



On 02.11.2011 01:32, Tony wrote:
> Hello, I need help getting two data sets to merge. The structure of
> my
> two data sets are:
>
>> str(bcusip)
> 'data.frame':   1391 obs. of  3 variables:
>   $ bond_id : Factor w/ 1391 levels "AAGH","AAGI",..: 1 2 3 4 5 6
>   $ Freq    : num  41361 4126 5206 10125 45536 ...
>   $ CUSIP_ID: Factor w/ 1391 levels "00184AAC9","00184AAF2",..:
>
>> str(bdescr)
> 'data.frame':   3674 obs. of  7 variables:
>   $ bond_id    : Factor w/ 3674 levels "AAGH ","AAGI ",..:
>   $ Issuer.Name: Factor w/ 635 levels "3M CO ","ABBOTT LABORAT
>   $ Coupon     : num  6 6.75 6.5 5.95 5.55 5.9 5.72 5.87 6 6.75 ...
>   $ Maturity   : Factor w/ 1076 levels "1/1/2015","1/1/2016",..:
>   $ Callable   : Factor w/ 2 levels "No ","Yes ": 2 2 2 2 2
>   $ Moody.s    : Factor w/ 20 levels "A1 ","A2 ","A3 ",..: 16 16 16
>   $ S.P        : Factor w/ 22 levels "- ","A- ","A ",..: 15 15 15 15 1


Look at the levels above, in "bcusip" ist is "AAGH", on "bdescr" it is 
"AAGH " (note the blanks! etc.

Uwe Ligges

> I am trying to attach the descriptive variables in the first data set
> to the
> sample variables in the second data set.
> My code worked in an example that I re-created from a tutorial, but
> it
> will not work on my data
> Here is my data code:
>
> ### bond description
> bdescr<-read.table(file="index3705.R.csv",header=TRUE,sep=",")
> bdescr<- bdescr[!duplicated(bdescr$bond_id),]
>
> ### bond cusip number
> bcusip = read.table(file="selected1526.R.csv",header=TRUE,sep=",")
> bcusip<- bcusip[!duplicated(bcusip$bond_id),]
> bcusip$Freq = as.numeric(bcusip$Freq)
>
> And here is my attempt to merge: (I tried a few)
>
> merge (bdescr,bcusip,by="bond_id",all=TRUE)
> merge (bdescr,bcusip,by="bond_id")
> merge (bdescr,bcusip)
> superfile<- merge(bdescr,bcusip,by="bond_id",all=TRUE)
>
> Thank you for any help. I am new and going crazy at the moment.
> Sincerely,
> Tony
>
> ______________________________________________
> 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