Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 行距离计算_R - Fatal编程技术网

R 行距离计算

R 行距离计算,r,R,我在r中有以下数据帧 count1 count2 count3 Count4 0 12 11 0 12 0 44 23 22 32 0 12 我想在行上应用的公式如下 1st row sqrt((count2-count3)^2) 2nd row sqrt((co

我在r中有以下数据帧

  count1      count2      count3    Count4
    0           12          11        0
    12          0           44        23
    22          32          0         12
我想在行上应用的公式如下

  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) 
我不想考虑具有零值的列。我有6个类似上面的专栏。我怎么能在r里做呢

申请循环并检查每一行的非零元素是一项乏味的任务

使用:

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)))
})
您可以将其缩短为:

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


使用数据:

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

如果我有6列呢?我必须在公式中更改什么?@Neil无需更改任何内容,这也适用于6列,因为您将此函数应用于所有行假设数字是
(160621573624)
公式是
sqrt((160-621)^2+(160-573)^2+(160-624)^2+(621-573)^2+(621-624)^2+(573-624)^2+)
根据您的代码,它将于1190年问世,实际是776@Neil将这些值作为额外的行添加到示例数据中;代码给出了正确的输出;见update@Neil将结果向量的所有值除以最大值,然后乘以100;另请参见更新,HTH
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)
[1]   0.1287459   5.1272551   3.1536171 100.0000000
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))
> 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