I’ve got 2 data frames – learn data with L rows and test data with T rows.
I want to compute a L*T matrix with distances (euclidean, manhattan, cosine…) between according elements.
Here is my take:
distance2 <- function (x1, x2) {
temp <- x1 - x2
sum(temp * temp)
}
m <- matrix(0,nrow(learnData),nrow(testData))
for(td in 1:nrow(testData)) {
for(ld in 1:nrow(learnData)) {
m[ld,td] <- distance2(testData[td,],learnData[ld,])
}
}
I think this can be done in a more compact, “R” way. Any ideas?
Thanks.
Two options spring to mind:
distance()which can compute the Euclidean and Manhattan measures for you on two data frames (but not the cosine distance).