Вы можете с помощью небольшого регулярного выражения вытащить соответствующие значения для формирования списка взвешенных ребер.
# parent node : grab the first number
parent <- sub("(\\d+ ).*$", "\\1", r)
# child node: grab everything after the first number and split it
child <- strsplit(sub("\\d+ (.*$)", "\\1", r), ":| ")
# cbind the parent node to the child
dat2 <- cbind(rep(parent, lengths(child)/2), matrix(unlist(child), nc=2, byrow=TRUE))
mode(dat2) = "numeric" # change to numeric
# read in a graph: the weights are in the edge attributes
g <- igraph::graph_from_data_frame(dat2)
Данные
Предполагая, что ваши данныеэтой формы в текстовом файле (измените textConnection(txt)
на путь к вашему файлу)
txt <-
'1 2:1.827411e-02 3:5.355330e-02 4:1.827411e-02 5:1.827411e-02
2 1:1.827411e-02 3:1.903553e-02 4:4.568528e-03 5:4.568528e-03
3 1:5.355330e-02 2:1.903553e-02 4:1.903553e-02 5:1.903553e-02 6:7.461929e-02 11:3.350254e-02
4 1:1.827411e-02 2:4.568528e-03 3:1.903553e-02 5:4.568528e-03
5 1:1.827411e-02 2:4.568528e-03 3:1.903553e-02 4:4.568528e-03
6 3:7.461929e-02 7:1.903553e-02 8:1.903553e-02 9:5.355330e-02 10:1.903553e-02 11:3.350254e-02
7 6:1.903553e-02 8:4.568528e-03 9:1.827411e-02 10:4.568528e-03
8 6:1.903553e-02 7:4.568528e-03 9:1.827411e-02 10:4.568528e-03
9 6:5.355330e-02 7:1.827411e-02 8:1.827411e-02 10:1.827411e-02
10 6:1.903553e-02 7:4.568528e-03 8:4.568528e-03 9:1.827411e-02
11 3:3.350254e-02 6:3.350254e-02'
r <- readLines(textConnection(txt))