Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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
&引用;“通过强制手段引入NAs”;在R_R_Cluster Analysis - Fatal编程技术网

&引用;“通过强制手段引入NAs”;在R

&引用;“通过强制手段引入NAs”;在R,r,cluster-analysis,R,Cluster Analysis,伙计们,我是这门语言的新手,我在一个数据帧上运行聚类分析,但当我计算距离时,我得到了这个警告“强制引入的NAs”。这是什么意思 d <- dist(as.matrix(mydata1)) Warning message: In dist(as.matrix(mydata1)) : NAs introduced by coercion 在这个除元族列之外的列中,所有列都是类中的数字列 伙计们,请帮我解决这个问题。这是第一个专栏,创建了这个问题: > a <- c("1",

伙计们,我是这门语言的新手,我在一个数据帧上运行聚类分析,但当我计算距离时,我得到了这个警告“强制引入的NAs”。这是什么意思

d <- dist(as.matrix(mydata1))

  Warning message:
In dist(as.matrix(mydata1)) : NAs introduced by coercion
在这个除元族列之外的列中,所有列都是类中的数字列


伙计们,请帮我解决这个问题。

这是第一个专栏,创建了这个问题:

> a <- c("1", "2",letters[1:5], "3")
> as.numeric(a)
[1]  1  2 NA NA NA NA NA  3
Warning message:
NAs introduced by coercion 
顺便说一句:调用
dist
时,不需要
as.matrix
。不管怎样,它在内部都会这样做

编辑:使用行名

rownames(df) <- df$id

> df
  id       var1       var2
A  A -0.6264538 -0.8204684
B  B  0.1836433  0.4874291
C  C -0.8356286  0.7383247
D  D  1.5952808  0.5757814
E  E  0.3295078 -0.3053884

> dist(df[-1]) # you colud also remove the 1st col at all, using df$id <- NULL.
         A        B        C        D
B 1.538458                           
C 1.572765 1.049697                  
D 2.624046 1.414400 2.436338         
E 1.085896 0.806124 1.564251 1.542284
rownames(df)df
id var1 var2
A-0.6264538-0.8204684
B 0.1836433 0.4874291
C-0.8356286 0.7383247
D 1.5952808 0.5757814
E 0.3295078-0.3053884

>dist(df[-1])#您还可以使用df$id@Michele-谢谢:)@Ravee删除第一个col,不客气。请使用
行名
用法检查编辑
> dist(df)
          1         2         3         4
2 1.8842186                              
3 1.9262360 1.2856110                    
4 3.2137871 1.7322788 2.9838920          
5 1.3299455 0.9872963 1.9158079 1.8889050
Warning message:
In dist(df) : NAs introduced by coercion
> dist(df[-1])
         1        2        3        4
2 1.538458                           
3 1.572765 1.049697                  
4 2.624046 1.414400 2.436338         
5 1.085896 0.806124 1.564251 1.542284
rownames(df) <- df$id

> df
  id       var1       var2
A  A -0.6264538 -0.8204684
B  B  0.1836433  0.4874291
C  C -0.8356286  0.7383247
D  D  1.5952808  0.5757814
E  E  0.3295078 -0.3053884

> dist(df[-1]) # you colud also remove the 1st col at all, using df$id <- NULL.
         A        B        C        D
B 1.538458                           
C 1.572765 1.049697                  
D 2.624046 1.414400 2.436338         
E 1.085896 0.806124 1.564251 1.542284