Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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_Matrix_Cluster Analysis - Fatal编程技术网

R 将相同的数字分配给矩阵中的类似行

R 将相同的数字分配给矩阵中的类似行,r,matrix,cluster-analysis,R,Matrix,Cluster Analysis,我有一个维数为N的方阵。我想定义一个大小为N的向量,它的第一个分量是: 与第一行相同的矩阵行的所有行索引。 作为第二部分: 与第二行相同的矩阵行的所有行索引 等等 我正在研究R,我已经尝试了一段时间了。如果您能告诉我如何进行,我将不胜感激 myMatrix <- matrix(rep(1:4, 4), ncol = 2, byrow = FALSE) [,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3 [4,] 4

我有一个维数为N的方阵。我想定义一个大小为N的向量,它的第一个分量是: 与第一行相同的矩阵行的所有行索引。 作为第二部分: 与第二行相同的矩阵行的所有行索引

等等

我正在研究R,我已经尝试了一段时间了。如果您能告诉我如何进行,我将不胜感激

myMatrix <- matrix(rep(1:4, 4), ncol = 2, byrow = FALSE)

     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    3    3
[4,]    4    4
[5,]    1    1
[6,]    2    2
[7,]    3    3
[8,]    4    4

myMatrixdplyr
version:

# turn the matrix into a dataframe
myDf <- myMatrix %>% as.data.frame()

myDf %>% # and now get a left join of ...
    left_join(
        myDf %>% # ...the same dataframe with the index you were looking for
            distinct_all() %>%
            mutate(index = 1:nrow(.)))
#将矩阵转换为数据帧
myDf%as.data.frame()
myDf%>%#现在得到一个左连接。。。
左联合(
myDf%>%#…与您要查找的索引相同的数据帧
完全不同的_all()%>%
突变(指数=1:nrow())

谢谢!我不习惯使用dplyr包。似乎是一个很好的包来处理数据帧!欢迎你可以在一本非常好的书的在线版本上获得更多关于如何使用
dplyr
作为“
tidyverse
”的信息。
# turn the matrix into a dataframe
myDf <- myMatrix %>% as.data.frame()

myDf %>% # and now get a left join of ...
    left_join(
        myDf %>% # ...the same dataframe with the index you were looking for
            distinct_all() %>%
            mutate(index = 1:nrow(.)))