我希望使用 Riverplot 包来创建流程图。这个包需要“边缘”,它们是层次之间的流动。我想从数据框创建边缘数据结构。举例来说,这里有一些代码来创建我的输入数据。
rp.df<-structure(list(ID = 1:20, X1 = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "A1", class = "factor"),
X2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A2",
"B2"), class = "factor"), X3 = structure(c(1L, 1L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
3L), .Label = c("A3", "B3", "C3"), class = "factor")), class = "data.frame", row.names = c(NA,
-20L))
table(rp.df$X1,rp.df$X2)
table(rp.df$X2,rp.df$X3)
有了这个输出
> table(rp.df$X1,rp.df$X2)
A2 B2
A1 12 8
> table(rp.df$X2,rp.df$X3)
A3 B3 C3
A2 2 2 8
B2 5 2 1
我需要的是一个数据框,其中包含表中标识的流,例如:
N1 N2 Value
A1 A2 12
A1 B2 8
A2 A3 2
A2 B3 2
A2 C3 8
B2 A3 5
B2 B3 2
B2 C3 1
实际上,我有 10 列边和 16k 流。我曾尝试使用 reshape2 来做到这一点,但很挣扎。