I have a data frame in R which is of size nx4. I am attempting to loop through it and perform a computation to add to the "distances" vector. x0 is a vector of length 3. I attempt to run the following code
trainData = data.frame(x1,x2,x3,y)
for (j in 1:n) {
distances[j] = sqrt(sum((x0 - trainData[j,1:3])^2))
}
I get the following error:
Error in Ops.data.frame(x0, trainData[j, 1:3]) :
‘-’ only defined for equally-sized data frames
However, the 2 values being subtracted are the same length, and I can run it without looping, ie
sqrt(sum((x0 - trainData[1,1:3])^2))
I'm unable to find the reason for this, any help is appreciated.