I would like to calculate the Manhattan distance using looping in r
Bill = c(2,3.5,2,3.5,3)
Ang = c(3.5,2,5,1.5,2)
user = cbind(Bill, Ang)
for (row in 1:nrow(user)){
for (col in 1:ncol(user)){
distance = 0
distance[row] = sum(abs(user[row,col] - user[row, col])))
}
}
I understand the code to do the followings: for the first loop:
for row equal to 1
for col equal to 1
distance = absolute sum of user[1,1] - user[1,2]
Output
#distance [1] NA NA NA NA 0
I know about the apply and other methods in the following link
Thank you for your help.