[R] Matching Problem: Want to match to data.frame with inexact matching identifier (one identifier has to be in the range of the other).

Christoph Jäckel christoph.jaeckel at wi.tum.de
Sat Apr 16 16:35:24 CEST 2011

Hello R-Community,

I have the following matching problem: I have two data.frames, one
with an observation every month (per company ID), and one with an
observation every quarter (per company ID; note that quarter means
fiscal quarter; therefore 1Q = Jan, Feb, Mar is not necessarily
correct and also, a fiscal quarter is not necessarily 3 month long).

For every month and company, I want to get the correct value of that
quarter. Consequently, several months have the same value for one
quarter. As an example see the code below:

#Monthly data
d1 <- data.frame(cbind(c(rep("A",5),rep("B",5)),c(1:5,1:5)),stringsAsFactors=FALSE)
names(d1) <- c("ID", "Month")
d1[,"Month"] <- as.integer(d1[,"Month"])

#Quarterly data, i.e. the value of every quarter has to be matched to
several months in d1
#However, I want to match fiscal quarters, which means that one
quarter is not necessarily 3 month long
d2 <- data.frame(cbind(c("A","A","B","B"),c(1,3,1,4),c(2,5,3,5),c("v1","v2","v3","V4")),stringsAsFactors=FALSE)
names(d2) <- c("ID", "Min_Month", "Max_Month","Value")
d2[,"Min_Month"] <- as.integer(d2[,"Min_Month"])
d2[,"Max_Month"] <- as.integer(d2[,"Max_Month"])

#Possible solution: Loop through every company and month and check in
which quarter (based on Min_Month and Max_Month) the current month is
for (i in 1:nrow(d1)){
  for (j in 1:nrow(d2)){
    if ((d1[i,"ID"] == d2[j,"ID"]) & (d1[i,"Month"]>= d2[j,"Min_Month"]) &
    (d1[i,"Month"]<= d2[j,"Max_Month"])) {break}
  d1[i,"Value"] <- d2[j,"Value"]

The solution works, but I was hoping that there would be a more
efficient and elegant one. I checked "match" and "merge", but didn't
figure out how I could use those to match without a unique identifier.

Any hints would be highly appreciated.



Christoph Jäckel (Dipl.-Kfm.)

Research Assistant
Chair for Financial Management and Capital Markets | Lehrstuhls für
Finanzmanagement und Kapitalmärkte
TUM School of Management | Technische Universität München

More information about the R-help mailing list