0

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.

Boro Dega
  • 393
  • 1
  • 3
  • 13
  • 4
    What exactly is your question? Why do you need to use a loop? What's your desired output? – Heroka Apr 12 '16 at 19:41
  • The question is how to calculate the manhattan distance through looping. That is the taking the row difference and sum them up. Please read the title for further information. – Boro Dega Apr 12 '16 at 19:49
  • 1
    Please consider reading up on [ask] and on [how to create a reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Heroka Apr 12 '16 at 19:56

1 Answers1

2

Not sure exactly what you're looking for, nor do I know what Manhattan distance is, but this may answer your question:

Bill <- c(2,3.5,2,3.5,3)
Ang  <- c(3.5,2,5,1.5,2)
Bob  <- c(4,2,5.5,1.5,3)
Dan  <- c(3,2,8,2.5,6.5)

user <- as.data.frame(cbind(Bill, Ang, Bob, Dan))


### Using numeric column references ###

for (j in 2:ncol(user)) {

   tmp     <- c(1:nrow(user))

   for (i in 1:nrow(user)) {

      tmp[i]  <- abs(user[i,1] - user[i,j])  

   }

   name <- paste0("dist_", names(user)[j])
   user <- cbind(user, tmp)
   names(user)[names(user)=="tmp"] <- name

}
cody_stinson
  • 390
  • 1
  • 3
  • 12
  • Great answer. Thanks!!!. Here you have already identified columns as "Bill" and "Ang". Isnt there an automatic approach in the for loop, where you could use the column number as an option. – Boro Dega Apr 12 '16 at 22:25
  • Not a problem. I've edited the answer to include using numeric column references. – cody_stinson Apr 12 '16 at 23:03
  • Thanks again, what I meant was what if I have 1000 columns and I wish to calculate the difference between rows of column 1 with the all other columns. Should it it be an argument like j in i:ncol(user) and use that to calculate the difference. Isnt there an automatic approach for it. Thanks – Boro Dega Apr 12 '16 at 23:10
  • Edited to include a dynamic example. That should do the trick. – cody_stinson Apr 12 '16 at 23:48
  • Got my answer!!Thanks – Boro Dega Apr 12 '16 at 23:51