You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言报错:cannot coerce type 'closure' to vector of type 'any'求解

错误原因及解决办法
  • 错误根源:代码里的dist是R的内置函数,直接在管道中写dist时,并没有调用这个函数处理前面的df,而是把dist这个函数对象(类型为closure)传递给了下一个hclust函数。但hclust需要的是距离矩阵(向量/矩阵类型),无法处理函数对象,因此触发cannot coerce type 'closure' to vector of type 'any'错误。
  • 修正后的代码:
library(dplyr)

dend1 <- df %>% 
  dist() %>% 
  hclust("complete") %>% 
  as.dendrogram

也可以写成dist(.),明确指定用管道传递的对象作为参数,效果和dist()一致。

内容的提问来源于stack exchange,提问作者Nanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 18:15:27