Edit: Code works just fine, was me that was the problem...
I want to merge several data.tables
into one. There are three columns
in each table, two coordinates x
, y
, and a Value assigned to the coordinates. However, the coordinates of the data.tables
differ slightly so I want to merge the nearest coordinates so I get
x y Value_1 Value_2 ... Value_i
I found this thread which seems to solve the problem. This solution was offered:
#Directly taken from the above mentioned thread:
func = function(u,v){
vec = with(DT2, (u-x)^2 + (v-y)^2)
DT2[which.min(vec),]$Q
}
transform(DT1, Q=apply(DT1, 1, function(u) func(u[1], u[2])))
which seemed to solve the problem.
However, if I use the following data:
#create data
library(data.table)
dt1 = data.table(X = runif(10000), Y = runif(10000), Value = runif(10000, min=5, max=10))
dt2 = data.table(X = runif(10000), Y = runif(10000), Value = runif(10000, min=5, max=10))
#Round data
dt1 = round(dt1, 3)
dt2 = round(dt2, 3)
setkey(dt1, "X","Y")
#Function from solution
func = function(u,v){
vec = with(dt2, (u-X)^2 + (v-Y)^2)
dt2[which.min(vec),]$Value_1
}
test = transform(dt1, Value=apply(dt1, 1, function(u) func(u[1], u[2])))
I get the following error message:
Error in data.table(list(X = c(0, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,:
column or argument 2 is NULL
this error message is also appearing when I use my "real" data which is quite similar to the created data above.
I am fairly new and have read quite bit about data.table
but haven't found a way that works. Any suggestions?