我有以下输入:
A B 0
A C 1
B C 1
我想将其转换为3x3矩阵:
A B C
A 0 0 1
B 0 0 1
C 1 1 0
我应该如何在R中执行此操作?以上只是为了说明我的目标。以下是我的数据。我将其减少到15个,但我的数据集有500个,并且自动收报机设置与ticker1和ticker2相同。因此,我想要一个n x n矩阵。
ticker1 ticker2 edge
AEPI AVAV 0.044210322
AEPI BOKF 0.008021954
AEPI CMSB 0.001986887
AEPI RON 0.006777085
AEPI CAKE 0.045002911
APPL COH 0.042760407
APPL SWZA 0.040913242
APPL DRIV 0.047699102
APPL CXP 0.014103206
MSCR FIVN 0.008781475
MSCR HOS 0.040222645
MSCR HDSN 0.010454853
MSCR IPWR 0.003376232
MSCR KELYB 0.000755429
答案 0 :(得分:3)
我们可以使用base R
方法。如果前两列不是factor
类,请将其转换为factor
并指定levels
,以使两列具有相同的levels
。然后使用xtabs
将其转换为'wide'格式,并通过添加'm1'的转置来更改下半部分的值
df1[1:2] <- lapply(df1[1:2], factor, levels = LETTERS[1:3])
m1 <- xtabs(v3~v1 + v2, df1)
names(dimnames(m1)) <- NULL
m1 + t(m1)
# A B C
#A 0 0 1
#B 0 0 1
#C 1 1 0
df1 <- structure(list(v1 = c("A", "A", "B"), v2 = c("B", "C", "C"),
v3 = c(0L, 1L, 1L)), .Names = c("v1", "v2", "v3"), class = "data.frame",
row.names = c(NA, -3L))
答案 1 :(得分:1)
igraph
的另一种方式和来自@akrun的数据
df1 <- structure(list(v1 = c("A", "A", "B"), v2 = c("B", "C", "C"),
v3 = c(0L, 1L, 1L)), .Names = c("v1", "v2", "v3"), class = "data.frame", row.names = c(NA, -3L))
library(igraph)
g=graph.data.frame(df1,directed=FALSE)
get.adjacency(g,attr='v3',spars=FALSE)
修改1
df2 <- read.table(text="ticker1 ticker2 edge
AEPI AVAV 0.044210322
AEPI BOKF 0.008021954
AEPI CMSB 0.001986887
AEPI RON 0.006777085
AEPI CAKE 0.045002911
APPL COH 0.042760407
APPL SWZA 0.040913242
APPL DRIV 0.047699102
APPL CXP 0.014103206
MSCR FIVN 0.008781475
MSCR HOS 0.040222645
MSCR HDSN 0.010454853
MSCR IPWR 0.003376232
MSCR KELYB 0.000755429",header=TRUE)
g=graph.data.frame(df2,directed=FALSE)
get.adjacency(g,attr='edge',spars=FALSE)