Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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,如果我从头开始创建矩阵,我可以以各种方式对其进行子集,保留列名: tmp2 <- matrix(sample(9), nrow = 3) colnames(tmp2) <- letters[1:3] tmp2 a b c [1,] 1 3 8 [2,] 7 2 6 [3,] 5 4 9 tmp2[2, 2:3] b c 2 6 tmp2[3, 1] a 5 tmp2区别在于,在第二行中有row.names。如果我们将它设置为NULL,那么它应该具有相同的行为

如果我从头开始创建矩阵,我可以以各种方式对其进行子集,保留列名:

tmp2 <- matrix(sample(9), nrow = 3)
colnames(tmp2) <- letters[1:3]

tmp2
     a b c
[1,] 1 3 8
[2,] 7 2 6
[3,] 5 4 9

tmp2[2, 2:3]
b c 
2 6 

tmp2[3, 1]
a 
5 

tmp2区别在于,在第二行中有row.names。如果我们将它设置为
NULL
,那么它应该具有相同的行为。默认情况下,
drop=TRUE
,我们得到的是一个名为
的向量。当同时存在row.name和column name时,这些属性将在
drop=TRUE
时被删除,因为命名向量只能有一个名称

row.names(tmpDM) <- NULL
tmpDM[3, 1]
#     a 
#9.539392 
现在,
c
是该向量的
名称,即行名称

这些值是不同的,因为没有设置
。seed
,每次都会从头开始重复


或者如果我们使用
drop=FALSE
,则
dim
将保持完整

tmpDM[3, 1, drop = FALSE]
#        a
#c 12.92285

哇,真管用。为什么?这有什么原因吗?这很有趣surprising@Tyler我更新了帖子。这可能与尺寸下降有关
colnames(tmpDM) <- NULL
tmpDM[3, 1]
#     c 
#5.567764 
tmpDM[3, 1, drop = FALSE]
#        a
#c 12.92285