Skip to content
Prev 990 / 7420 Next

How to index different datasets in the common period based on their same column?

There is certainly no need for a loop. Here's one way of doing this,
though I'm sure others will come up with something more elegant.

# create dummy data
data1 <- matrix(c(1550:2005, rnorm(456)), 456, 2)
data2 <- matrix(c(1959:2008, rnorm(50)), 50, 2)

# identify common subsets
sub1 <- data1[,1] %in% data2[,1]
sub2 <- data2[,1] %in% data1[,1]

# compute correlation
cor(data1[sub1,2], data2[sub2,2])