我有一个列表,其中包含图表的节点程度,如下所示:
degree <- list()
degree[[4]]<-degree(graph[[4]],v=V(graph[[4]]),mode="all")
> degree[[4]]
62586 4654 353 1883 7241 2680 1369 11942 21516 19922 20777 1137 251 3188 26013 7444
3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2927 3439 65369 21737 16897 185
1 1 1 1 1 1
我需要将矢量列表中的每个矢量(此处为[[4]])转换为矩阵。我尝试过以下方法:
matrix4 <- matrix(degree[[4]], nrow = length(degree[[4]]))
> matrix4
[,1]
[1,] 3
[2,] 1
[3,] 1
[4,] 1
[5,] 1
[6,] 1
[7,] 1
[8,] 1
[9,] 1
[10,] 1
[11,] 1
[12,] 1
[13,] 1
[14,] 1
[15,] 1
[16,] 1
[17,] 1
[18,] 1
[19,] 1
[20,] 1
[21,] 1
[22,] 1
但我还需要矩阵中的节点名称。
> str(degree[[4]])
Named num [1:22] 3 1 1 1 1 1 1 1 1 1 ...
- attr(*, "names")= chr [1:22] "62586" "4654" "353" "1883" ...
matrix4 <- cbind(degree[[4]]$names,matrix(degree[[4]], nrow = length(degree[[4]])))
不起作用,因为degree [[4]] $ names会给我一个错误。
任何帮助都将不胜感激。
答案 0 :(得分:0)
尝试
matrix4 <- matrix( degree[[4]],
nrow = length(degree[[4]]),
dimnames=list(names(degree[[4]])) )
例如
> matrix4 <- matrix(degree[[4]], nrow = length(degree[[4]]), dimnames=list(names(degree[[4]])))
> degree[[4]]<-c(4,3,4,2)
> names(degree[[4]])=c("a","b","cc","ddd")
> degree[[4]]
a b cc ddd
4 3 4 2
> matrix4 <- matrix(degree[[4]], nrow = length(degree[[4]]), dimnames=list(names(degree[[4]])))
> matrix4
[,1]
a 4
b 3
cc 4
ddd 2