Neil Neil - 1 month ago 7
R Question

row wise distance calculation

I have following dataframe in r

count1 count2 count3 Count4
0 12 11 0
12 0 44 23
22 32 0 12


Formula that I want to apply on row wise is like this

1st row sqrt((count2-count3)^2)
2nd row sqrt((count1-count3)^2 + (count1-count4)^2 + (count3-count4)^2)
3rd row sqrt((count1-count2)^2 + (count1-count4)^2 + (count2-count4)^2)


I do not want to take columns with zero values into consideration. I have 6 columns like above. How can I do it in r?

Applying for loop and checking for every row for non zero elements is tedious task.

Answer Source

Using:

apply(df, 1, function(x) {
  y <- x[x!=0]
  yc <- combn(y,2)
  sqrt(sum(apply(yc, 2, function(x) (x[1] - x[2])^2)))
})

gives:

[1]   1.00000  39.82462  24.49490 776.72389

You can shorten this to:

apply(df, 1, function(x) {
  sqrt(sum(apply(combn(x[x!=0],2), 2, function(x) (x[1] - x[2])^2)))
})

Used data:

df <- structure(list(count1 = c(0L, 12L, 22L, 160L), count2 = c(12L, 0L, 32L, 621L), 
                     count3 = c(11L, 44L, 0L, 573L), count4 = c(0L, 23L, 12L, 624L)), 
                .Names = c("count1", "count2", "count3", "count4"), class = "data.frame", row.names = c(NA, -4L))

which looks like:

> df
  count1 count2 count3 count4
1      0     12     11      0
2     12      0     44     23
3     22     32      0     12
4    160    621    573    624