【发布时间】:2021-08-03 18:07:21
【问题描述】:
我想制作一个网络图来表示数据库中表之间的连接。我有一个包含以下列的数据框:
- 表名
- 列名
- column_id
我不确定迭代数据框以生成边缘表的最佳方法,该边缘表创建从 - 到“column_name”相同的列(即 column_id 1 和 7)。我的数据框有 1,500 多行。
任何帮助或指导将不胜感激。
# Example dataframe with example values
df <- as.data.frame(rbind(
c("table_A", "column1", 1),
c("table_A", "column3", 2),
c("table_A", "column5", 3),
c("table_B", "column3", 4),
c("table_B", "column5", 5),
c("table_B", "column6", 6),
c("table_C", "column1", 7),
c("table_D", "column3", 8),
c("table_D", "column5", 9),
c("table_E", "column1", 10),
c("table_E", "column6", 11)
))
colnames(df)[1] <- "table_name"
colnames(df)[2] <- "column_name"
colnames(df)[3] <- "column_id"
# Example desired output using column_id
edges <- as.data.frame(rbind(
c(1,7),
c(1,10),
c(7,10),
c(2,4),
c(2,8),
c(4,8),
c(3,5),
c(3,9),
c(5,9),
c(6,11)
))
colnames(edges)[1] <- "from"
colnames(edges)[2] <- "to"
【问题讨论】:
标签: r dataframe relationship igraph