相关疑难解决方法(0)

如何使用核心R操纵/访问"dist"类实例的元素?

R中的基本/公共类被称为"dist",并且是对称距离矩阵的相对有效的表示."matrix"但是,与对象不同,似乎不支持"dist"使用"["运算符通过索引对操作实例.

例如,以下代码不返回任何内容NULL,或错误:

# First, create an example dist object from a matrix
mat1  <- matrix(1:100, 10, 10)
rownames(mat1) <- 1:10
colnames(mat1) <- 1:10
dist1 <- as.dist(mat1)
# Now try to access index features, or index values
names(dist1)
rownames(dist1)
row.names(dist1)
colnames(dist1)
col.names(dist1)
dist1[1, 2]
Run Code Online (Sandbox Code Playgroud)

同时,在某种意义上,以下命令可以正常工作,但不要使访问/操作特定索引对值更容易:

dist1[1] # R thinks of it as a vector, not a matrix?
attributes(dist1)
attributes(dist1)$Diag <- FALSE
mat2 <- as(dist1, "matrix")
mat2[1, 2] <- 0
Run Code Online (Sandbox Code Playgroud)

一个解决方法 - …

indexing r class distance matrix

20
推荐指数
3
解决办法
1万
查看次数

按列名称将表转换为矩阵

我的数据框如下所示

       models cores     time
1       4     1 0.000365
2       4     2 0.000259
3       4     3 0.000239
4       4     4 0.000220
5       8     1 0.000259
6       8     2 0.000249
7       8     3 0.000251
8       8     4 0.000258
Run Code Online (Sandbox Code Playgroud)

......等

我想将其转换为表格/矩阵,其中#models用于行标签,#cores用于列标签,时间用作数据条目

例如

  1 2 3 4 5 6 7 8    
1   time data
4   time data
Run Code Online (Sandbox Code Playgroud)

目前我正在使用for循环将其转换为此结构,但我想知道是否有更好的方法?

statistics r

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

r ×2

class ×1

distance ×1

indexing ×1

matrix ×1

statistics ×1