英文:
Create a dendrogram
问题
我有这样的数据集
| SELF | OTHER |
| -------- | -------- |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 2 | 6 |
| 2 | 1 |
| 2 | 5 |
| 3 | 5 |
| 3 | 4 |
| 3 | 2 |
| 4 | 8 |
| 4 | 7 |
| 4 | 5 |
| 5 | 1 |
| 5 | 2 |
| 5 | 3 |
| 6 | 7 |
| 6 | 8 |
| 6 | 5 |
| 7 | 5 |
| 7 | 2 |
| 7 | 3 |
| 8 | 6 |
| 8 | 6 |
| 8 | 6 |
自我代表8名足球运动员,其他代表这8名足球运动员中的一个与哪些人交友。我想评估两名足球运动员的友谊圈有多相似,以及共同的朋友。然后我想做一个聚类。
到目前为止我做了这些,但是出现了错误消息
footballplayers <- read.csv("footballplayer.csv", header = TRUE)
footballplayers_e <- graph_from_edgelist(as.matrix(footballplayers),directed = TRUE)
similarities <- as.dist(1 - cor(footballplayers_e), upper = TRUE)
footballplayersdend<-hclust(similarities)
plot(footballplayersdend)
但是它不起作用。
Error in FUN(X[[i]], ...) : as.sociomatrix.sna input must be an adjacency matrix/array, network, or list.
英文:
I have a dataset like this
SELF | OTHER |
---|---|
1 | 3 |
1 | 4 |
1 | 5 |
2 | 6 |
2 | 1 |
2 | 5 |
3 | 5 |
3 | 4 |
3 | 2 |
4 | 8 |
4 | 7 |
4 | 5 |
5 | 1 |
5 | 2 |
5 | 3 |
6 | 7 |
6 | 8 |
6 | 5 |
7 | 5 |
7 | 2 |
7 | 3 |
8 | 6 |
8 | 6 |
8 | 6 |
The self represent the 8 football players and the other represents with which of this 8 football players one is befriended. I one to evaluate how similar two football players have a friendship circle, also share same people as friends. And then I would like to do a clustering
This is what I did so far, but got error message
footballplayers <- read.csv("footballplayer.csv", header = TRUE)
footballplayers_e <- graph_from_edgelist(as.matrix(footballplayers),directed = TRUE)
similarities <- as.dist(1 - cor(footballplayers_e), upper = TRUE)
footballplayersdend<-hclust(similarities)
plot(footballplayersdend)
But it doesn't work.
Error in FUN(X[[i]], ...) : as.sociomatrix.sna input must be an adjacency matrix/array, network, or list.
答案1
得分: 0
从边缘列表获取邻接矩阵的简单方法是使用Matrix::sparseMatrix
函数。
library(tibble)
df <- tribble(~SELF, ~OTHER,
1, 3,
1, 4,
1, 5,
2, 6,
2, 1,
2, 5,
3, 5,
3, 4,
3, 2,
4, 8,
4, 7,
4, 5,
5, 1,
5, 2,
5, 3,
6, 7,
6, 8,
6, 5,
7, 5,
7, 2,
7, 3,
8, 6,
8, 6,
8, 6)
with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE))
##> 8 x 8 sparse Matrix of class "ngCMatrix"
##>
##> [1,] . . | | | . . .
##> [2,] . . . . | | . .
##> [3,] | . . | | . . .
##> [4,] | . | . | . | |
##> [5,] | | | | . . . .
##> [6,] . | . . . . | |
##> [7,] . . . | . | . .
##> [8,] . . . | . | . .
然后我们可以应用 dist
和 hclust
,例如:
with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE)) |>
dist("binary") |>
hclust() |>
plot()
英文:
An easy way to get an adjacency matrix from an edge list is by using Matrix::sparseMatrix
library(tibble)
df <- tribble(~SELF, ~OTHER,
1, 3,
1, 4,
1, 5,
2, 6,
2, 1,
2, 5,
3, 5,
3, 4,
3, 2,
4, 8,
4, 7,
4, 5,
5, 1,
5, 2,
5, 3,
6, 7,
6, 8,
6, 5,
7, 5,
7, 2,
7, 3,
8, 6,
8, 6,
8, 6)
with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE))
##> 8 x 8 sparse Matrix of class "ngCMatrix"
##>
##> [1,] . . | | | . . .
##> [2,] . . . . | | . .
##> [3,] | . . | | . . .
##> [4,] | . | . | . | |
##> [5,] | | | | . . . .
##> [6,] . | . . . . | |
##> [7,] . . . | . | . .
##> [8,] . . . | . | . .
Then we can apply dist
and hclust
e.g.:
with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE)) |>
dist("binary") |>
hclust() |>
plot()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论