【问题标题】:How to calculate the distance among series of multiple points in data table using R?如何使用R计算数据表中多个点系列之间的距离?
【发布时间】:2019-06-04 17:41:15
【问题描述】:

我想使用 R 计算系列中多行之间的距离。 有 967 X 35 的数据表。 表格数组如下。

1. 6.23  3.3   4.36  3.9    ----  4.50   1.50  3.35   (35 column)
2. 5.00  2.3   3.36  4.39   ----  2.52   3.40  2.37   (35 column)
3. 5.23  2.6   5.64  4.23   ----  3.50   4.55  3.48   (35 column)

如果我想计算每行的每个单元格之间的距离怎么办? 例如,假设我要计算第 1 行和第 2 行之间的距离/第 2 行和第 3 行之间的距离等等。

那么计算距离的数学公式将是... {(6.23-5)^2 +(3.3-2.3)^2+(4.36-3.36)^2+(3.9-4.39)^2+------- + (4.5-2.52)^2+(1.5-3.4)^2+(3.35-2.37)^2} 的平方根,每隔两行(1-2 行、2-3 行、3-4 行、……、967-1 行(967 次))进行相同的计算

但我不知道如何在 R 中编写这种方法。

我上传了数据表。 (称为“R_skills”)

然后使用R,我编码如下。

  1. 'Read.xlsx(R_skills)' 2.sample.matrix<-matrix(c(1:33635,ncol=35)
  2. paralleldist(x=sample.mtarix,method="dtw")

错误:出现意外符号: “sample.matrix

【问题讨论】:

  • 您的问题对于 StackOverflow 的形成并不好,请根据 stackoverflow.com/editing-help 的建议进行编辑。但是,乍一看,您可能在sample.matrix<-matrix... 调用结束时缺少一个右括号:您有两个左边,但只有一个右边。如果这不是它或不够,请考虑使这个问题更容易和更可重复:请以明确的格式发布您的示例数据(例如,dput(head(x)),并将您的代码放入代码块中。stackoverflow.com/questions/5963269 , stackoverflow.com/help/mcve.

标签: r


【解决方案1】:

这应该为你做:

sapply(1:(nrow(dt)-1),function(t,dt){dist(dt[t:(t+1),])},dt)

【讨论】:

    【解决方案2】:

    @tushaR 对基础 R 来说是正确的。:)

    mat <- sample.matrix <- matrix(1:33635,ncol=35)
    
    dist_fun <- function(x, y) sqrt(sum(x-y)**2)
    
    s_fun <- function(t){
            sapply(t, function(x) dist_fun(mat[x,], mat[x+1]) )
    }
    m_fun <- function(t){
            mapply(function(x) dist_fun(mat[x,], mat[x+1]), t)
    }
    a_fun <- function(t){
            apply(matrix(t, nrow = 1), 2, function(x) dist_fun(mat[x,], mat[x+1]) )
    }
    l_fun <- function(t){
            unlist( lapply(as.list(t, nrow = 1), function(x) dist_fun(mat[x,], mat[x+1]) ) )
    }
    
    # t <- 1:(nrow(mat)-1)
    # s_fun( 1:(nrow(mat)-1) )
    
    library(microbenchmark)
    n <- 1e5 # 961
    mat <- sample.matrix <- matrix(1:(35*n),ncol=35)
    
    microbenchmark("sapply" = s_fun(1:(nrow(mat)-1)),
                   "mapply" = m_fun(1:(nrow(mat)-1)),
                   "apply" = a_fun(1:(nrow(mat)-1)),
                   "lapply" = l_fun(1:(nrow(mat)-1)),
                   list = NULL, times = 100L, unit = "ms", check = NULL,
                   control = list(), setup = NULL)
    #> Unit: milliseconds
    #>    expr      min       lq     mean   median       uq      max neval
    #>  sapply 315.5892 413.6667 534.0462 498.0186 600.1315 1096.092   100
    #>  mapply 313.7013 441.9728 534.9250 505.3026 577.5770 1167.973   100
    #>   apply 387.1655 503.9833 615.6288 563.4751 665.4584 1571.387   100
    #>  lapply 309.3762 416.0796 553.0482 491.5356 645.2026 1823.269   100
    

    由reprex package (v0.2.1) 于 2019-06-04 创建

    【讨论】:

      猜你喜欢
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2022-11-16
      • 2013-02-08
      • 1970-01-01
      相关资源
      最近更新 更多