I'm trying to find the overall distance moved by a worker and my df looks something like
Name x y John 12 34 John 15 31 John 8 38 John 20 14 I've tried using the dist(rbind()) function, but the result given is not correct. It just gives the result of sqrt((row1)^2+(row2)^2+(row3)^2+(row4)^2), which I don't think is correct.
So I'm trying to use for loop to do this, so that dist between row 1 and 2 , 2 and 3, and so on is calculated separately and summed up later. How would I do this?
My code currently looks like:
for(i in nrow(df)){ n <- dist(rbind(df$x,df$y)) } and this just gives me the wrong single result mentioned above, and not a list of individual distances for each 1-2 row/s.
My expected output would be like:
4.2426 9.8995 26.8328 and I can sum them up later by I guess running:
sum(n) right?
ianywhere in the loop. Probably , you are trying to do ,n[i] <- dist(rbind(df$x[i],df$y[i])). Currently,nis holding only one value at any given time and probably, the entire thing can be replaced without a loop. I am not sure though, as it is not clear to me.