Neil
Neil

Reputation: 8247

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.

Upvotes: 1

Views: 203

Answers (1)

Jaap
Jaap

Reputation: 83215

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)))
})

In response to your comment:

out <- 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)))
})

100*out/max(out)

gives:

[1]   0.1287459   5.1272551   3.1536171 100.0000000

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

Upvotes: 2

Related Questions